0

如何从 SQL 表的列中获取所有数据并与从表单中获取的数据进行检查?这是我到目前为止所拥有的:

<?php
 $con=mysqli_connect("www.tqbtest.comlu.com","a5349216","Password","a5349216_test");
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

 //How do I check the data from the form to make sure there is no username/email already used?

 $sql="INSERT INTO Profiles (firstName, lastName, username, email, password, region, profileGroup, verified)
 VALUES
 ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

 if (!mysqli_query($con,$sql))
   {
   die('Error: ' . mysqli_error($con));
   }
 echo "1 record added";

 mysqli_close($con);


?>
4

2 回答 2

0

你有几个选择:

  • 您可以将唯一索引添加到您希望唯一的任何字段。它们将导致任何违反您的唯一性约束的插入失败,然后您可以捕获并反馈给您的应用程序。
  • 您可以尝试对您的表进行选择,以获得您希望唯一的字段,然后查看是否返回结果。如果这样做,您可以将错误返回给您的用户。
于 2013-06-30T00:06:40.283 回答
0

我认为这是你需要的:

$DBsql = mysql_query("select col1, col2 from table_name where some_id = $some_id");
$sql = mysql_fetch_array($DBsql);

if(($_POST['field1'] == $sql['col1']) && ($_POST['field2'] == $sql['col2'])) {
    echo 'The fields are valid';
} else {
    echo 'One or more fields are not valid';
}
于 2013-06-30T00:18:14.157 回答