在SQL表中同时插入两行中的值

时间:2016-08-29 08:28:57

标签: php sql database

这是我的php文件我想同时在两行中插入状态值。

<?php
include_once 'db_connect.php';
if(isset($_POST['btn-signup']))
{
    $PersonID = mysql_real_escape_string($_POST['PersonID']);
    $FirstName = mysql_real_escape_string($_POST['FirstName']);
    $Status = mysql_real_escape_string($_POST['Status']);

    $uname = trim($uname);
    $email = trim($email);
    $upass = trim($upass);

    // email exist or not
    $query = "SELECT PersonID FROM attandance WHERE PersonID='$PersonID'";
    $result = mysql_query($query);

    $count = mysql_num_rows($result); // if email not found then register

    if($count == 0){
        if(mysql_query("INSERT INTO state(PersonID,FirstName,Status)    VALUES('$PersonID','$FirstName','$Status')"))
        { ?>
            <script>alert('successfully Added ');</script>
            <?php
        }
        else
        { ?>
            <script>alert('error while registering you...');</script>
            <?php
        }       
     }
     else
     {
     ?>
        <script>alert('Sorry PersonID already taken ...');</script>
        <?php
     }
 }

echo "<!DOCTYPE html>\n"; 
echo "<html>\n"; 
echo "<head>\n"; 
echo "  <meta charset=\"utf-8\">\n"; 
echo "  <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n"; 
echo "  <title>AdminLTE 2 | Blank Page</title>\n";
echo "</head>\n";
echo "<body>\n";

echo "          \n"; 
echo "\n";
echo "\n";
include_once 'dbconnect.php';
$query  = "SELECT * FROM attandance ";
$result = mysql_query($query);
echo "<table border=\"2\">\n"; 
echo "   <tr>\n"; 
echo "      <th>PersonId</th>\n"; 
echo "      <th>First Name</th>\n";
echo "      <th>Status</th>\n"; 


echo "    </tr>\n";
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{   
    $PersonID=$row['PersonID'];
    $FirstName=$row['FirstName'];
    echo "<td> <input type=\"text\" name=\"user_id\" value=\" $PersonID\">   </td>\n"; 

    echo "<td> <input type=\"text\" name=\"full_name\" value=\" $FirstName\"></td>\n"; 
    echo "<td> <input type=\"text\" name=\"full_name\" value=\" $Status\"></td>\n"; 

    echo "</tr>\n";
}

echo " </table>\n";
echo "<input id=\"button\" type=\"submit\" name=\"btn-signup\" value=\"Sign-Up\">\n";

echo "</body>\n";
echo "</html>\n";
echo "\n";
?>

我想使用按钮一次添加两个字段的状态。但它没有用。谁能帮我。在状态表中,我想同时向两行添加值。

1 个答案:

答案 0 :(得分:0)

错误代码:)但

你读这篇文章

$PersonID = mysql_real_escape_string($_POST['PersonID']);
$FirstName = mysql_real_escape_string($_POST['FirstName']);
$Status = mysql_real_escape_string($_POST['Status']);

和此输入

echo "<td> <input type=\"text\" name=\"user_id\" value=\" $PersonID\">   </td>\n"; 

echo "<td> <input type=\"text\" name=\"full_name\" value=\" $FirstName\"></td>\n"; 
echo "<td> <input type=\"text\" name=\"full_name\" value=\" $Status\"></td>\n"; 

更改输入的名称,试试这个

echo "<td> <input type=\"text\" name=\"PersonID\" value=\" $PersonID\">   </td>\n"; 

echo "<td> <input type=\"text\" name=\"FirstName\" value=\" $FirstName\"></td>\n"; 
echo "<td> <input type=\"text\" name=\"Status\" value=\" $Status\"></td>\n"; 

表格在哪里?请阅读:http://php.net/manual/en/tutorial.forms.php