2016-09-27 53 views
-1

我想運行一個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!'; 
} 

任何幫助,得到這個工作表示讚賞。

+0

是你的Serial_Number列是在整數類型的數據庫? –

+0

我也試着去掉這個WHERE(Serial_Number ='$ serial')的括號,再加上它們不需要。 –

+0

您嘗試過空($ dupe);而不是$ num_rows == 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")); 

它完美地工作。

感謝那些幫助我找出代碼出錯的地方。