2014-07-01 137 views
0

PHP还很新,但学习很快。我有两种形式收集数据,然后传递给php函数。所有来自这两个表单的数据都将它传递给php文件,因为我正在回应这些值。通过php插入多个表单数据到多个mysql表

我的问题是第一个表正确更新没有任何问题,但第二个表没有更新。

这里是

private function registerNewUser($user_name, $user_email, $user_password, $user_password_repeat, $captcha, $user_type, $first_name) 
.... 

// write new users data into database 
    $query_new_user_insert = $this->db_connection->prepare('INSERT INTO users (user_name, user_password_hash, user_email, user_activation_hash, user_registration_ip, user_registration_datetime, user_type) VALUES(:user_name, :user_password_hash, :user_email, :user_activation_hash, :user_registration_ip, now(), :user_type)');     
    $query_new_user_insert->bindValue(':user_name', $user_name, PDO::PARAM_STR); 
    $query_new_user_insert->bindValue(':user_password_hash', $user_password_hash, PDO::PARAM_STR); 
    $query_new_user_insert->bindValue(':user_email', $user_email, PDO::PARAM_STR); 
    $query_new_user_insert->bindValue(':user_activation_hash', $user_activation_hash, PDO::PARAM_STR); 
    $query_new_user_insert->bindValue(':user_registration_ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR); 
    $query_new_user_insert->bindValue(':user_type', $user_type, PDO::PARAM_STR);     
    $query_new_user_insert->execute();    

// id of new user 
    $id = $this->db_connection->lastInsertId(); 

    echo $first_name; 
    echo $user_email; 
    echo $id; 
// attempt at writing to additional table 
    $this->db_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 
    $query_new_user_insert2 = $this->db_connection->prepare('INSERT INTO C_Customer (First_Name, Email_Address, Created_Date, id) VALUES(:first_name, :user_email, now() :id'); 
    $query_new_user_insert2->bindValue(':first_name', $first_name, PDO::PARAM_STR); 
    $query_new_user_insert2->bindValue(':user_email', $user_email, PDO::PARAM_STR); 
    $query_new_user_insert2->bindValue(':id', $id, PDO::PARAM_INT); 
    $query_new_user_insert2->execute(); 

$query_new_user_insert作品表更新

$query_new_user_insert2作为表C_Customers不包含任何数据不工作有问题的代码。

+0

您应该对调试PDO查询做一些研究。你可以从这里开始:https://stackoverflow.com/questions/2411182/how-to-debug-pdo-database-queries – pmandell

+0

谢谢你的建议,因为我现在看到的错误。我更新了上面的代码以显示它现在的样子。当我现在运行这个时,我得到以下错误(53的值是users表中的id是正确的) '警告:PDOStatement :: execute():SQLSTATE [42000]:语法错误或访问冲突:1064您在您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,在“53”附近使用正确的语法。错误输出的行是'$ query_new_user_insert2-> execute();' – Jxmccall

+0

在第二个查询中,您的':id'参数在“值”括号之外。投票结束为简单的错字。请确保您的所有数据库访问都有错误处理。您会看到您记下的语法错误,并且可能会在比撰写本文时短的时间内解决问题。 –

回答

0

第二次查询时,您忘记了在now():id之间加逗号。此外,你忘了把右括号。

prepare('INSERT INTO C_Customer (First_Name, Email_Address, Created_Date, id) VALUES(:first_name, :user_email, now() :id') 

应该

prepare('INSERT INTO C_Customer (First_Name, Email_Address, Created_Date, id) VALUES(:first_name, :user_email, now(), :id)'); 

另一个错字:)希望它帮助。

+0

谢谢!!就是这样。我想我需要走开一段路,因为我正在犯许多错字。我不能将你的回答标记为正确,因为我没有这样的声望。但是这解决了我的问题。 – Jxmccall