无法插入到表中。PHP/MySQL
我在数据库“db2”中有一个名为“Directors”的表。 我有一个HTML表单。当我插入值并点击submit按钮时,我希望在表进行一些验证(您会在脚本中注意到它们)之后,将内容插入到表中的新行(插入)。我试着自己去做,但它总是回应我“失败”; 这是我的HTML表单:无法插入到表中。PHP/MySQL,php,mysql,Php,Mysql,我在数据库“db2”中有一个名为“Directors”的表。 我有一个HTML表单。当我插入值并点击submit按钮时,我希望在表进行一些验证(您会在脚本中注意到它们)之后,将内容插入到表中的新行(插入)。我试着自己去做,但它总是回应我“失败”; 这是我的HTML表单: <form action="process.php" method="post" accept-charset="utf-8"> <input type="hidden" name="pages_edi
<form action="process.php" method="post" accept-charset="utf-8">
<input type="hidden" name="pages_edit_nonce" />
<div class="section-item page-title-section">
<label for="title">Full Name:</label><span class="help">*</span><div class="input-wrap"><input type="text" name="name" value="" /></div> </div>
<div class="section-item">
<label for="label">Phone:</label><span class="help">*Optionally</span><div class="input-wrap"><input type="text" name="phone" value="" /></div> </div>
<div class="section-item">
<label for="redirect">Е-mail:</label><span class="help">*</span><div class="input-wrap"><input type="text" name="email" value="" placeholder="" /></div> </div>
<div class="section-item">
<label for="redirect">School:</label><span class="help">*</span><div class="input-wrap"><input type="text" name="school" value="" placeholder="" /></div> </div>
<div class="section-item">
<label for="redirect">City:</label><span class="help">*</span><div class="input-wrap"><input type="text" name="city" value="" placeholder="" /></div> </div>
<div class="section-item">
<label for="redirect">Password:</label><span class="help">*</span><div class="input-wrap"><input type="password" name="password" value="" placeholder="" /></div> </div>
<div class="admin-bar">
<div class="admin-bar-inner">
<input type="submit" value="Submit" class="btn" />
</div>
</div>
</form>
全名:*
电话:*可选
电子邮箱:*
学校:*
城市:*
密码:*
这是我的process.php文件:
$server = "localhost";
$user = "****";
$pass = "****";
$conn = mysql_connect($server, $user, $pass);
$db = mysql_select_db("****", $conn);
session_start();
if(!$db) {
$_SESSION['ERRMSG'] = "<strong>Error:</strong> The access to the database is denied!";
header("Location: ../../admin/error/");
exit();
}
session_start();
function UniqueID() {
$UID = rand(); //Create unique ID
$check = mysql_query("SELECT * FROM `Directors` WHERE `UID` = '$UID'");
if(mysql_num_rows($check) > 0) { //Check if it exists
UniqueID(); //Redo the function
} else {
return $UID; //return the uniqueid
}
}
$UID = UniqueID(); //Unique ID
$email = $_POST['email'];
$password = $_POST['password'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$school = $_POST['school'];
$city = $_POST['city'];
//Create INSERT query
$qry = "INSERT INTO `oclass`.`Directors`(`UID`,`Name`, `Phone`, `Email`, `SchoolGymnasium`, `City`, `Password`) VALUES('$UID','$name','$phone','$email','$school','$city','" . md5($password) . "')";
$result = mysql_query($qry);
//Check whether the query was successful or not
if($result) {
$_SESSION['SUCCMSGADDDIR'] = 'Sucessful.';
header("location: URL");
exit();
} else {
$_SESSION['ERRMSGADDDIR'] = 'Fail';
header("location: URL");
}
$server=“localhost”;
$user=“****”;
$pass=“****”;
$conn=mysql\u connect($server、$user、$pass);
$db=mysql\u select\u db(“****”,$conn);
会话_start();
如果(!$db){
$\u会话['ERRMSG']=“错误:对数据库的访问被拒绝!”;
标题(“位置:../../admin/error/”;
退出();
}
会话_start();
函数UniqueID(){
$UID=rand();//创建唯一ID
$check=mysql\u查询(“从'Directors'中选择*,其中'UID`='$UID'”);
如果(mysql_num_rows($check)>0){//检查它是否存在
UniqueID();//重做函数
}否则{
return$UID;//返回uniqueid
}
}
$UID=UniqueID()//唯一ID
$email=$_POST['email'];
$password=$_POST['password'];
$name=$_POST['name'];
$phone=$_POST['phone'];
$school=$_POST['school'];
$city=$_POST['city'];
//创建插入查询
$qry=“插入到`oclass`.`Directors`(`UID`、`Name`、`Phone`、`Email`、`schoolkysium`、`City`、`Password`)值(`UID`、`Name`、`Phone`、`Email`、`school`、`City`、`md5($Password`)。”;
$result=mysql\u查询($qry);
//检查查询是否成功
如果($结果){
$\会话['succmsgaddir']=“成功”;
标题(“位置:URL”);
退出();
}否则{
$\会话['errmsgaddir']='Fail';
标题(“位置:URL”);
}
使用mysql_error()更改错误会话后,它给了我以下错误:
致命错误:无法在中的写入上下文中使用函数返回值。。。第10行;
第10行是:
mysql_error() = "<strong>Error:</strong> The access to the database is denied!";
mysql\u error()=“错误:对数据库的访问被拒绝!”;
我已经删除了名为ID的列(它是主键)并将UID列设置为主键,现在正在工作。谢谢大家。首先,你们一定从未听说过SQL注入。您当前的代码使您面临攻击。不能像现在这样直接将用户输入插入数据库。此外,mysql_*函数也不推荐使用。要帮助您的代码更加安全和更新,请尝试以下操作:
session_start();
$host = "localhost";
$user = "****";
$pass = "****";
$db = "****";
$dbh = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("INSERT INTO `oclass`.`Directors`(`UID`,`Name`, `Phone`, `Email`, `SchoolGymnasium`, `City`, `Password`) VALUES (:uid, :name, :phone, :email, :school, :city, :password)");
$stmt->bindParam(':uid', uniqid());
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':phone', $_POST['phone']);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':school', $_POST['school']);
$stmt->bindParam(':city', $_POST['city']);
$stmt->bindParam(':password', md5($_POST['password']));
$stmt->execute();
如果您一直得到“Fail”,这意味着条件If($result)返回false,这意味着$result在这种情况下不是有效的资源,这是由错误的查询(mysql错误)造成的。回音MySQL错误,看看你得到什么。考虑使用<代码> PDO < /代码>或<代码> MySqLI>代码>而不是<代码> MySqL**<代码>。无论如何,为了调试查询,请将失败会话的值更改为:
mysql\u error()
,并共享输出。要将$\u会话['succmsgaddir']更改为mysql\u error(),连接到数据库时出现问题!!您确定用户名和密码吗?如果uid是您的主键,请从uid列中删除PI(我的意思是不要将uid用作主键),或者删除insert语句中的uid