第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將數(shù)據(jù)插入到 2 個獨立的 mySQL 表中

將數(shù)據(jù)插入到 2 個獨立的 mySQL 表中

PHP
翻閱古今 2023-09-22 16:59:07
下面是我正在嘗試做的事情的簡化版本。本質(zhì)上我有 2 個注冊表,每個表單的內(nèi)容都將寫入不同的表。當(dāng)我加載表單時,我在第 xx 行上收到許多錯誤未定義索引,并且在本示例的第 75 行和第 84 行中仍然收到錯誤,我認(rèn)為錯誤歸結(jié)于某些“必需”字段,但該頁面尚未加載為它使用一些 JS 就飄走了。... 請幫忙!<?phpob_start(); //Turns on output bufferingsession_start(); //This starts a session variable where it store the input so the user doesnt have to start over if they get an error$con = mysqli_connect("localhost","root","","test-sql"); //connection variables    $timezone = date_default_timezone_set("Europe/London");     if(mysqli_connect_errno()){        echo "failed to connect:" . mysqli_connect_errno();    }?><html>    <head>            <! here we add the links and the jquery>    <title>Register Project</title>    </head>    <body>                <div id="first">                    <form action="test-sql.php" method="POST">                        <input type="text" name="Inf_fname" placeholder="First Name" required>                        <br>                        <input type="text" name="Inf_lname" placeholder="Last Name" required>                        </form>                    </div>                <div id="second">                                    <form action="test-sql.php" method="POST">                                               <br>                    <input type="text" name="Cust_fname" placeholder="First Name" required>                    <br>                                                                   <input type="text" name="Cust_lname" placeholder="Last Name" required>                    <br>                        <input type="submit" name="register_button" value="Register">                                               </form>                    </div>    </body>    </html><?php$inf_fname = "";$cust_fname = "";$inf_lname = "";$cust_lname = "";if(isset($_POST['register_button'])) {        $inf_fname = strip_tags($_POST['Inf_fname']);    $inf_fname = str_replace(' ', '', $inf_fname);    $_SESSION['Inf_fname'] = $inf_fname;    }
查看完整描述

1 回答

?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗 獲得超6個贊

由于您一次僅提交一種格式,因此無法插入到兩個表中。您需要檢查提交的是哪個表單,并插入到相應(yīng)的表中。每個表單都需要一個具有不同名稱或值的提交按鈕,這樣您就可以知道在 PHP 中使用了哪一個。


您還應(yīng)該使用準(zhǔn)備好的語句,而不是將變量替換到 SQL 中,以防止 SQL 注入。我已經(jīng)展示了如何做到這一點。


<?php

ob_start(); //Turns on output buffering

session_start(); //This starts a session variable where it store the input so the user doesnt have to start over if they get an error

$con = mysqli_connect("localhost","root","","test-sql"); //connection variables

  

$timezone = date_default_timezone_set("Europe/London");

  

if(mysqli_connect_errno()){

    echo "failed to connect:" . mysqli_connect_errno();

}

?>


<html>

<head>

<! here we add the links and the jquery>

<title>Register Project</title>

</head>

<body>

        

<div id="first">

    <form action="test-sql.php" method="POST">

    <input type="text" name="Inf_fname" placeholder="First Name" required>

    <br>

    <input type="text" name="Inf_lname" placeholder="Last Name" required>

    <input type="submit" name="inf_submit" value="Register">

    </form>

</div>


<div id="second">

                

    <form action="test-sql.php" method="POST">

                        

    <br>

    <input type="text" name="Cust_fname" placeholder="First Name" required>

    <br>

                                               

    <input type="text" name="Cust_lname" placeholder="Last Name" required>

    <br>

    

    <input type="submit" name="cust_submit" value="Register">

                        

    </form>

</div>

</body>


</html>


 <?php


 $inf_fname = "";

 $cust_fname = "";

 $inf_lname = "";

 $cust_lname = "";


 if(isset($_POST['inf_submit'])) {


     $inf_fname = strip_tags($_POST['Inf_fname']);

     $inf_fname = str_replace(' ', '', $inf_fname);

     $_SESSION['Inf_fname'] = $inf_fname;

     $inf_lname = strip_tags($_POST['Inf_lname']);

     $inf_lname = str_replace(' ', '', $inf_lname);

     $_SESSION['Inf_lname'] = $inf_lname;


     $stmt = mysqli_prepare($con, "INSERT INTO inf VALUES (?, ?)");

     mysqli_stmt_bind_param($stmt, "ss", $inf_fname, $inf_lname);

     mysqli_stmt_execute($stmt);

 } elseif (isset($_POST['cust_submit'])) {


     $cust_fname = strip_tags($_POST['Cust_fname']);

     $cust_fname = str_replace(' ', '', $cust_fname);

     $_SESSION['Cust_fname'] = $cust_fname;

     $cust_lname = strip_tags($_POST['Cust_lname']);

     $cust_lname = str_replace(' ', '', $cust_lname);

     $_SESSION['Cust_lname'] = $cust_lname;


     $stmt = mysqli_prepare($con, "INSERT INTO customer VALUES (?, ?)");

     mysqli_stmt_bind_param($stmt, "ss", $cust_fname, $cust_lname);

     mysqli_stmt_execute($stmt);

 }

 ?>



查看完整回答
反對 回復(fù) 2023-09-22
  • 1 回答
  • 0 關(guān)注
  • 105 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號