提交表单时,它在mysql中输入多行

问题描述

一个有用的成员已经建议的以下测试代码会引起一些意外的问题。首先,当我提交“ cust_submit”按钮时,由于某种原因,它会将多个条目输入数据库,而不仅仅是一行输入。

其次,我想在“ inf_submit”部分添加一个自动递增的ID,但会引发错误“只能通过引用在其中传递变量”


<?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();
}

if ($con->connect_error){
    die("Connection Failed: ". $con->connect_error);
    } echo "connected successfully";
?>

<html>
<head>
<!-- here we add the links and the jquery-->
<title>Register Project</title>
</head>
<body>
        
<div id="first">
    <form action="fromstackoverview.PHP" method="POST">
    <input type="text" name="Inf_fname" placeholder="First Name" required>
    <br>
    <input type="text" name="Inf_lname" placeholder="Last Name" required>
    <br>
    <input type="submit" name="inf_submit" value="Register">
        
    </form>
</div>

<div id="second">
                
    <form action="fromstackoverview.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="text" name="cust_email" placeholder="Email" required>
    <br>
    <input type="submit" name="cust_submit" value="Register">
                        
    </form>
</div>
</body>

</html>

 <?PHP

 $inf_fname = "";
 $cust_fname = "";
 $inf_lname = "";
 $cust_lname = "";
 $cust_em = "";


 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,"sss",$inf_fname,$inf_lname);
     MysqLi_stmt_execute($stmt);
 } else if (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;
     $cust_em = strip_tags($_POST['cust_email']); //removes html tags
     $cust_em = str_replace(' ',$cust_em); //remove spaces in name
     $cust_em = ucfirst(strtolower($cust_em)); //This will keep the first letter upper case and lower case everything else
    $_SESSION['cust_email'] = $cust_em; //Stores input into session variable

     $stmt = MysqLi_prepare($con,"INSERT INTO customer VALUES (?,$cust_fname,$cust_lname,$cust_em);
     MysqLi_stmt_execute($stmt);
 }
 ?>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)