我正在教自己php和MySQL,现在我遇到了MySQL问题。
我想比较用户输入的电话号码和MYSQL中的电话号码,以及是否在MYSQL中再次注册。
我的代码:
<?php
require_once 'connection/connection.php';
// Variables from HTML to php
$worker_Name = $_POST['workerNameFromHtml']; // worker Name
$worker_City = $_POST['workerCityFromHtml']; // workerCity
$worker_career = $_POST['workerCareerFromHtml']; // worker career
$worker_PhoneNumber = $_POST['workerPhonNumberFromHtml']; // worker Phone Number
$worker_SecondPhoneNumber = $_POST['workerSecondPhoneNumberFromHtml']; // worker Second Phone Number
$submt=$_POST['submitFromHtml'];
if($submt){
$qry = ( "SELECT workrPhoneNumber FROM workersTable WHERE workrPhoneNumber = '$worker_PhoneNumber'") or die(mysql_error());
$result = $connect->query($qry);
$num = $result->num_rows;
if ($num == 1) {
$here = "INSERT INTO workersTable VALUES('','$worker_Name','$worker_City','$worker_career','$worker_PhoneNumber','$worker_SecondPhoneNumber')";
$query = $connect->query($here);
print "Successfully added!";
}
else {print "This number has already been entered Thank you for your cooperation!";}}
$connect->close();
到目前为止,我还没有找到解决这个问题的方法。
答案 0 :(得分:0)
这里最大的问题是你试图在字符串中包含变量。
"SELECT workrPhoneNumber FROM workersTable WHERE workrPhoneNumber = '$worker_PhoneNumber'"
如果你想这样做,你需要将你的变量与你的字符串连接起来。
"SELECT workrPhoneNumber FROM workersTable WHERE workrPhoneNumber = '".$worker_PhoneNumber."'"
请记住,如果这样做,您将首先要清理变量以防止SQL注入。另外,当你INSERT变量时,你实际上想要使用这样的预准备语句:
"INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)"
其中第一组值是数据库中列的名称,第二组是您放入其中的PHP变量。