2016-09-28 33 views
0

我是新来的PHP,但试图学习新的东西。我有三个字段的数据库中的表。 ID,城市和名称。我想要的是通过从文本框中获取值来检查所有这三个字段是否相等,然后显示错误,否则它会显示我重定向到新页面的链接。但我不能够正常运行此查询试图像几件事情算等验证检查3个字段来检查重复

<form> 
<input id="text1" type="text" name="id" size="20" > 
<input id="text1" type="text" name="name" size="20" > 
<input id="text1" type="text" name="city" size="20" > 
<input id="text1" type="submit" name="submit" size="20" > 
</form> 

<?php 
$con=mysqli_connect("localhost","root","","test"); 
$check="SELECT * FROM eval WHERE id = '$_POST[id]' AND name = '$_POST[name]' AND city = '$_POST[city]' "; 

if(if all this data is same then) { 
echo "Record already exist...<br/>"; 
} 
else 
{ 
echo "<a href='NewUser.php'> New Sign Up </a> 
} 
?> 
+0

任何想法,将不胜感激,以获得所需的结果,查询可以是任何东西,但需要路由到用户之前,检查所有这三场下一页... – A1Nasir

回答

1
<form> 
    <input id="text1" type="text" name="id" size="20" > 
    <input id="text1" type="text" name="name" size="20" > 
    <input id="text1" type="text" name="city" size="20" > 
    <input id="text1" type="submit" name="submit" size="20" > 
</form> 

<?php 
    $con=mysqli_connect("localhost","root","","test"); 
    $check="SELECT COUNT(*) FROM eval WHERE id = '$_POST[id]' AND name = '$_POST[name]' AND city = '$_POST[city]' "; 
    $result = mysqli_query($con,$check); 
    $count = mysqli_fetch_array($result); 
    if(current($count) > 0) { 
     echo "Record already exist...<br/>"; 
    } 
    else 
    { 
     echo "<a href='NewUser.php'> New Sign Up </a> 
    } 
?> 

你不得不解雇使用mysqli_query()SQL查询到数据库。之后检查行数是否大于0,这意味着您找到了一个或多个精确重复的行。

SELECT COUNT(*)自动计算检索到的行。

+0

不工作...在页面加载此查询显示我其他语句...你可以请检查...我想运行此提交按钮来检查验证 – A1Nasir

+0

@ A1Nasir当然它。把它包装成一个'if(isset($ _ POST ['submit'])){}'或者类似的东西...... – Xatenev

1

好的,你有一个HTML表单和一些PHP代码来处理表单提交时发生的情况。

因此,当表单被提交时,为了PHP注册此提交需要知道该表单已提交。

如果您正在通过JavaScript处理表单输入,但您在PHP中有一些不同,您可以在客户端上使用表单,但在PHP中它有点不同。

所以你的形式将需要两个额外的东西:

1)指定的表单数据将如何被发送的方法属性。

2.)定义表单数据将被发送到何处的动作。

最常用的方法是GET和POST。而当你有PHP代码处理与表单相同页面上的表单时,这个动作就是这个页面本身。所以,你现在有:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <input type="text" name="id"> 
    <input type="text" name="name"> 
    <input type="text" name="city"> 
    <input type="submit" name="clicked" value="formSubmit"> 
</form> 

在PHP为了您注册此输入,您可以使用提交按钮被点击时发生的事件。所以,你将有:

<?php 
    # If the submit button was clicked 
    if (isset($_POST['clicked']) && $_POST['clicked'] == 'formSubmit') { 
    # Access the form inputs in php 
    $id = $_POST['id']; 
    $name = $_POST['name']; 
    $city = $_POST['city']; 

    # Helper to output mysql errors 
    function processError($link) { 
     echo "Error: Unable to connect to MySQL.<br>"; 
     echo "Debugging errno: " . $link->errno . "<br>"; 
     echo "Debugging error: " . $link->error . "<br>"; 
     exit; 
    } 

    # Connect to the database 
    // Create a database connection for PHP to use 
    $link = mysqli_connect($DBHost, $DBUser, $DBPass, $DBName); 
    // Ensure the connection is active 
    if (!$link) { 
     echo "Error: Unable to connect to MySQL.<br>"; 
     echo "Debugging errno: " . mysqli_connect_errno() . "<br>"; 
     echo "Debugging error: " . mysqli_connect_error() . "<br>"; 
     exit; 
    } 
    // Sets encoding type to uft8 
    if (!mysqli_set_charset($link, 'utf8')) { processError($link); } 

    # Do query 
    // Build query 
    $query = 'SELECT `id` '; 
    $query .= 'FROM `eval` '; 
    $query .= 'WHERE `id` = ? '; 
    $query .= 'AND `name` = ? '; 
    $query .= 'AND `city` = ? '; 
    $query .= 'LIMIT 1 '; 
    // Prepare the statement 
    if (!$stmt = $link->prepare($query)) { processError($link); } 
    // Bind in form values to prevent sql injection 
    if (!$stmt->bind_param('iss', $id, $name, $city)) { processError($link); } 
    // Execute the query 
    if (!$stmt->execute()) { processError($link); } 
    // Store the result 
    $stmt->store_result(); 
    // Store the number of rows returned 
    $num_rows = $stmt->num_rows; 
    // Close the statement 
    $stmt->close(); 

    # Check if the record exists 
    if ($num_rows == 1) { 
     echo 'Record already exist...<br/>'; 
    } 
    // No record exists 
    else { 
     echo '<a href="NewUser.php"> New Sign Up </a>'; 
    } 
    } 
?> 

希望帮助你:-)