0

出于某种原因,除了 ID 之外,没有任何东西被发送到我的数据库

HTML

 <form action="sendmessage.php" method="post">

 <input name="youremail" type="text" class="your-field" id="youremail" value="Your Email Address" size="35"  />

 <input name="name" type="text" class="their-field" id="name" value="Receivers Name" size="35" onclick="if(this.value == 'Receivers Name') { this.value = ''; }" />

 <input name="receiveremail" type="text" class="their-field" id="receiveremail" value="Receivers Email Address" size="35" onclick="if(this.value == 'Receivers Email Address') { this.value = ''; }" />

 <textarea name="message" cols="35" rows="5" class="valentine-message" id="textarea" onclick="if(this.value == 'Your Message') { this.value = ''; }" />Your Message</textarea>

 <input class="button" name="" type="image" src="images/button.jpg" onClick="submit')">

 </form>

PHP

 // creates a random number for the id, ans check to see if the random number currently exists in database
 $success = FALSE; 

 while($success == FALSE) { 
$rand = rand(100000, 999999); 

$q = "SELECT * FROM $tablename WHERE rand = '$rand'"; 
$r = mysql_query($q, $link);

echo mysql_error();


if(mysql_num_rows($r)) { //id exists 
    continue; 
     } else { 
         $success = TRUE; 
     } 
 } 

 // insert your data here with $rand as the id
 $query = "INSERT into $tablename values ('$rand', '$youremail', '$name', '$receiveremail', '$message')";
 $result = mysql_query($query, $link);

 if (!$result) {
echo "Query Failed: " . mysql_error() . "<br />\n";
exit;
 }

这是数据库结构http://i50.tinypic.com/14jq3he.png你能看到任何问题吗?

4

1 回答 1

0

首先,此代码不能在 PHP 版本 >= 5.4 中工作,因为register_globals它已关闭。在几乎所有其他系统上, register_globals 都已关闭,因为这是一个很大的安全问题。

最好使用 $_POST['fieldname']。

第二 - 您的代码容易受到 sql 注入的攻击

最后,不应该再使用 mysql_* 函数,因为它们已经过时并且被 pdo 或 mysqli_* 取代,它们可以处理 sql 注入。

如果您修复了这三件事(或至少前两件事),您的代码应该可以工作或显示有用的错误消息。

最后一件事-如果启用警告,您会看到出了什么问题

于 2013-02-05T01:30:37.427 回答