2013-03-06 34 views
3

這是我的代碼:文本框在HTML

<html> 
<body> 

    <BR><p><U>Add contact...</U></p><BR> 
    Name:   <INPUT TYPE="text" NAME="firstbox"> 
    Id.number:  <INPUT TYPE="text" NAME="idbox"> 

    <?php 
    session_start(); 

    $username=$_SESSION['UserID']; 
    $s_name=$_GET['firstbox']; 
    $s_id=$_GET['idbox']; 
    $db = mysql_connect("???????", "???????", ""); 
    mysql_select_db("???????",$db); 

    $result = mysql_query("SELECT * FROM contacts WHERE u_id='$username' and c_id='$s_id'",$db); 

    $myrow = mysql_fetch_row($result); 

    if(!$myrow) { 
     mysql_query("INSERT INTO contacts(u_id, c_name, c_id) VALUES ('$username','$s_name','$s_id')",$db); 
     header("Location: http://?????/protectedpage.php"); 
    } else { 
     header("Location: http://?????contact1.htm"); 
    } 
    ?> 

</body> 
</html> 

是否有可能來檢查,如果我看重的firstboxlastbox進入,如果是,連接到數據庫,但如果沒有,請不要將進入數據庫?

是否有可能限制在一個框中的整數,例如,假設在idbox,如果整數大於5(12345,11234,999900),不要插入數據庫?

好,我都試過,但它不工作

if (($s_id="") and ($_name="")) 
{ 
header("Location: http://at-web2.comp.glam.ac.uk/students/10003088/contact1.htm"); 

} 
else 
{ 
$db = mysql_connect("at-web2.comp.glam.ac.uk", "user_10003088", "Glamorgan1"); 
mysql_select_db("db_10003088",$db); 

$result = mysql_query("SELECT * FROM contacts WHERE u_id='$username' and c_id='$s_id'",$db); 

$myrow = mysql_fetch_row($result); 


if(!$myrow) 
    { 
    mysql_query("INSERT INTO contacts(u_id, c_name, c_id) VALUES ('$username','$s_name','$s_id')",$db); 

    header("Location: http://at-web2.comp.glam.ac.uk/students/10003088/protectedpage.php"); 
    } 
else{ 

    header("Location: http://at-web2.comp.glam.ac.uk/students/10003088/contact1.htm"); 
} 
} 

我添加了一個if語句來檢查,

+1

是否有可能一個'<!DOCTYPE HTML>'添加到您的代碼,以便它看起來並不像在IE的魁完整的廢話rks模式?除此之外,是的,你問的是可能的,但[你有什麼嘗試](http://whathaveyoutried.com/)? – 2013-03-06 15:09:32

+1

是的,這是可能的,自己嘗試一下,讓我們知道你卡住了哪裏。 – codefreak 2013-03-06 15:09:46

+2

此外,請升級到mysqli或PDO語句,mysql *函數集已在PHP – Husman 2013-03-06 15:10:42

回答

2

的回答您的所有問題是「是」。

+0

如何簡潔。 :-D – 2013-03-06 15:50:56

+2

但它確實回答了這個問題:) :) – 2013-03-06 16:22:09

0
  1. 要檢查是否文本框爲空,把一個If語句爲每一個(使用而不是「文字」文本框名稱:

    $firstbox = $_POST['firstbox']; 
    If ($firstbox === "") { 
        Echo "firstbox Empty"; 
    } 
    
    $idbox = $_POST['idbox']; 
    If ($idbox === "") { 
        Echo "idbox Empty"; 
    } 
    
  2. 要檢查idbox的價值,使用類似的if語句:

    $idbox = $_POST['idbox']; 
    If (intval($idbox) > 5) { 
        Echo "idbox value greater than 5"; 
    }