douzhang6646 2015-07-13 21:14
浏览 63
已采纳

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

This is for a registration form I have created. I cannot include all of my code, but the program checks for blank fields then format using the preg_match function. Then it INSERTS the info registered

My code is:

<?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");
}
?>

How do I move just the variable to the query and not the whole INSERT string?

  • 写回答

1条回答 默认 最新

  • duandi1636 2015-07-13 21:36
    关注

    There's no need to call mysqli_fetch or mysqli_fetch_assoc. Just do the UPDATE query outside of a loop:

    $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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路