如何从php中的INSERT查询中提取变量?

时间:2015-07-13 21:14:47

标签: php mysql

这是我创建的注册表单。我无法包含所有代码,但程序会检查空白字段,然后使用preg_match函数进行格式化。然后它会记录注册的信息

我的代码是:

<?php
 /* connection info */
ini_set('include_path','../../includes');
include("dbinfo.inc");
$cxn = mysqli_connect($host,$user,$password,$dbname) 
    or die("Couldn't connect to server. error 3");
?>


<?php
/*  Program name: Register.php 
 *  Description:  Program displays the blank form and checks 
 *  all the form fields for blank fields.
 */


 // Insert info into database //
    {
    foreach($good_data as $field => $value)
        {
            $good_data[$field] = mysqli_real_escape_string($cxn,$value);
        }

    $sql = "INSERT INTO UserInfo (user_id, password, first_name, last_name, city, country, email) VALUES ('$good_data[user_id]', '$good_data[password]', '$good_data[first_name]', '$good_data[last_name]', '$good_data[city]', '$good_data[country]', '$good_data[email]')";

        $result = mysqli_query($query, $sql) or die ("Couldn't connect to login");
        $row = mysqli_fetch($result);

        while ($row = mysql_fetch_assoc($result)) {

            $sql2 = "UPDATE TimeStamp SET time = CURRENT_TIMESTAMP where user_id='$good_data[user_id]'";  
               $result2 = mysqli_query($cxn,$sql2) or die("<p>Couldn't Connect to Login</p>");
                       include('goodReg.inc');

        }
        {
            echo $message;
            extract($good_data);
            include('register.inc');
            exit();
        } 
    }   
}
else
{
  include("register.inc");
}
?>

如何仅将变量移动到查询而不是整个INSERT字符串?

1 个答案:

答案 0 :(得分:0)

无需致电mysqli_fetchmysqli_fetch_assoc。只需在循环外执行UPDATE查询:

$sql = "INSERT INTO UserInfo (user_id, password, first_name, last_name, city, country, email) VALUES ('$good_data[user_id]', '$good_data[password]', '$good_data[first_name]', '$good_data[last_name]', '$good_data[city]', '$good_data[country]', '$good_data[email]')";
mysqli_query($cxn, $sql) or die ("Couldn't insert into UserInfo: " . mysqli_error($cxn));

$sql2 = "UPDATE TimeStamp SET time = CURRENT_TIMESTAMP where user_id='$good_data[user_id]'";  
mysqli_query($cxn, $sql2) or die ("Couldn't update TimeStamp: " . mysqli_error($cxn);

include('goodReg.inc');