PHP提交数据前检查序列号(数据)
问题描述:
我想运行一个php上传,它检查序列号是否已经存在,如果它没有提交数据。PHP提交数据前检查序列号(数据)
这样:
提交表单 - > PHP检查数据库的重复序列号 - >如果序列号不存在,则发布数据//如果确实存在,忽略输入
我尝试但是什么都似乎没有工作。以下是我的代码,但不管我做了什么,即使序列号已经存在,也会提交数据。
形式:
<form id="form1" action="senddata.php" name="form1" method="post">
<table class="table2" cellpadding="0" cellspacing="0">
<tr><td colspan="3"><input type="button" onClick="update()" value="Get Details"></td></tr>
<tr>
<td><label for="description">Description:</label></td>
<td><input tabindex="0" required type="text" name="description" id="description"></td>
</tr>
<tr>
<td><label for="nameofcomputer">Computer Code:</label></td>
<td><input tabindex="1" required type="text" name="nameofcomputer" id="nameofcomputer"></td>
</tr>
<tr>
<td><label for="make">Make:</label></td>
<td><input tabindex="2" required type="text" name="make" id="make"></td>
</tr>
<tr>
<td><label for="model">Model:</label></td>
<td><input tabindex="3" required type="text" name="model" id="model"></td>
</tr>
<tr>
<td><label for="serial">Serial Number:</label></td>
<td><input tabindex="4" required type="text" name="serial" id="serial"></td>
</tr>
<tr>
<td><label for="inputname">Your Name: </label></td>
<td><input tabindex="5" required id="inputname" name="inputname">
</td>
</tr>
</table>
<input required type="submit" name="submit" id="submit" value="Submit">
</form>
PHP
$desc=$_POST['description'];
$code=strtoupper($_POST['nameofcomputer']);
$make=$_POST['make'];
$model=$_POST['model'];
$serial=strtoupper($_POST['serial']);
$user=$_POST['inputname'];
$type='1';
$org='-1';
$control = "8670";
$now = new DateTime(null, new DateTimeZone('Europe/London'));
$date = $now->format('Y-m-d H:i:s');
$conn = sqlsrv_connect($serverName, $connectionInfo);
$dupe = sqlsrv_query($conn, "SELECT * FROM Asset WHERE Serial_Number = '$serial'");
$num_rows = sqlsrv_num_rows($dupe);
if ($num_rows == 0) {
$tsql = "INSERT INTO Asset (Name, Asset_Type_ID, Ref_Code, Owner_Organisation_ID, Make, Model, Serial_Number, Current_Location, Start_Date)
VALUES ('$desc', '$type', '$code', '$org', '$make', '$model', '$serial', '$user', '$date')";
sqlsrv_query($conn, $tsql);
echo "Asset Uploaded";
} else {
echo 'Error! Already on our database!';
}
任何帮助,得到这个工作表示赞赏。
答
我更换
$dupe = sqlsrv_query($conn, "SELECT Serial_Number FROM Asset WHERE Serial_Number = '$serial'");
与
$dupe = sqlsrv_query($conn, "SELECT Serial_Number FROM Asset WHERE Serial_Number = '$serial'", array(), array("Scrollable"=>"buffered"));
它完美地工作。
感谢那些帮助我找出代码出错的地方。
是你的Serial_Number列是在整数类型的数据库? –
我也试着去掉这个WHERE(Serial_Number ='$ serial')的括号,再加上它们不需要。 –
您尝试过空($ dupe);而不是$ num_rows == 0? –