在提交数据之前,PHP检查序列号(数据)

时间:2016-09-27 13:59:59

标签: php sqlsrv

我正在尝试运行一个php上传,它检查序列号是否已经存在,然后它是否提交数据。

所以:

提交表单 - > PHP检查DB是否有重复的序列号 - >如果序列号不存在则发布数据//如果存在则忽略输入

然而,我尝试的东西似乎并不起作用。以下是我的代码,但无论我做什么,它都会提交数据,即使序列号已经存在。

表格:

    <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!';
}

任何有助于实现这一目标的帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

我替换了

$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"));

它完美无缺。

感谢那些帮我弄清楚代码出错的人。