我只是編程中的新手,我不知道我的代碼中存在什麼問題。我在tblins中輸入的數據被記錄下來,但tbluser中的數據不會出現在我的數據庫中。但是當我嘗試刪除我的tblins的插入查詢時,我想要在tbluser中輸入的數據可以記錄在我的數據庫中。我應該怎麼做才能讓我的兩個表格可以記錄我在頁面中點擊提交後所輸入的所有數據?謝謝。php sql插入表
$usr="INSERT INTO tbluser(username,password,type) VALUES('".$_POST['txtuname']."','".$_POST['txtpass']."','".$_POST['type']."')";
$ins="INSERT INTO tblins(insLN,insFM,insMN,insadd,insCN,insemail,insbdate) VALUES('".$_POST['txtLN']."','".$_POST['txtFN']."','".$_POST['txtMN']."','".$_POST['txtadd']."','".$_POST['txtCN']."','".$_POST['txtemail']."','".$bdate."')";
感謝所有的建議先生。 :D我目前準備好什麼SQL注入。希望瞭解更多。 :D
這是我的完整代碼。
<?php
include("connect.php");
if(isset($_POST['txtpass']) && isset($_POST['txtrepass'])){
$password1=mysql_real_escape_string($_POST['txtpass']);
$password2=mysql_real_escape_string($_POST['txtrepass']);
if($password1==$password2){
$typeopt=$_POST['type'];
$bdate=$_POST['year']."-".$_POST['month']."-".$_POST['day'];
switch($typeopt){
case 'ins':
$usr=mysql_query("INSERT INTO tbluser(username,password,type) VALUES('".$_POST['txtuname']."','".$_POST['txtpass']."','".$_POST['type']."')");
$ins=mysql_query("INSERT INTO tblins(insLN,insFM,insMN,insadd,insCN,insemail,insbdate) VALUES('".$_POST['txtLN']."','".$_POST['txtFN']."','".$_POST['txtMN']."','".$_POST['txtadd']."','".$_POST['txtCN']."','".$_POST['txtemail']."','".$bdate."')");
if(mysqli_query($con,$ins)) {
echo"success";
}
else{
echo"fail to register";
}
break;
case 'student':
$std="INSERT INTO tblstudent(studLN,studFN,studMN,studBDate,studemail,studadd,studCN)";
$usr="INSERT INTO tbluser(username,password,type)";
$usr=$usr."VALUES('".$_POST['txtuname']."',";
$usr=$usr."'".$_POST['txtpass']."',";
$usr=$usr."'".$_POST['type']."')";
$std=$std."VALUES('".$_POST['txtLN']."',";
$std=$std."'".$_POST['txtFN']."',";
$std=$std."'".$_POST['txtMN']."',";
$std=$std."'".$bdate."',";
$std=$std."'".$_POST['txtemail']."',";
$std=$std."'".$_POST['txtadd']."',";
$std=$std."'".$_POST['txtCN']."')";
if(mysqli_query($con,$std)) {
echo"success";
}
else{
echo"fail to register";
}
}
}
else{
echo"<form>";
echo "Password doesn't match. Try registering again.";
echo "<input type=submit formaction=register.php value=back>";
echo"</form>";
}
}
?>
你可以在你試圖將這些代碼寫入數據庫的地方添加代碼嗎? – andrewsi
你應該檢查任何PHP錯誤或MySQL錯誤。有可能有一個錯誤信息告訴你*確切的*有什麼問題。另外,你的代碼對SQL注入攻擊是全面的。你需要閱讀和理解這個:http://php.net/manual/en/security.database.sql-injection.php – David
這應該是你的數據庫連接錯誤。在這裏粘貼你的完整代碼 –