duanquanzhi5560 2014-12-19 13:59
浏览 26
已采纳

左连接和最后插入ID困境

hi in want to build a registration and lottery app with php i want to make a users id in my prize table upon registration of users and i want to add to a number of my buy_count when users buy a ticket...

so i got 2 tables :

prize , users

prize : users_id , buy_count
users : fn , ln , pass...usual staff

this is the sctipt for users reg form :

$sql = 'INSERT INTO users (id,fn,ls,psw) VALUES (:id,:fn,:ln,:psw);
INSERT INTO prize (user_id, buy_count) VALUES (LAST_INSERT_ID(), 4)';

script gives me an error , whats wrong with it ? i want to add to users id in prize table upon user registration , whats the problem with my query ? whats the right way to do this ?


UPDATED :

so now this is my code :

<?php



try {
    require_once 'inc/connect.php';

    $sql =("
INSERT INTO users (fn,ls,psw) VALUES (:fn,:ln,:psw);
INSERT INTO prize (user_id) VALUES (LAST_INSERT_ID());
    ");



$form = $_POST;
$fn = $form['fn'];
$ln = $form['ln'];
$psw = $form['psw'];

    $values = array(
':fn' => $fn ,
':ln' => $ln ,
':psw' => $psw

    );

    $stmt = $db->prepare($sql);
    $stmt->execute($values);



    if ( $stmt ){
        echo "<p>Thank you. You have been registered</p>";
    } else {
        echo "<p>Sorry, there has been a problem inserting your details. Please contact admin.</p>";
    }

    $errorInfo = $db->errorInfo();
    if (isset($errorInfo[2])){
        $error = $errorInfo[2];
    }



} catch (Exception $e) {
    $error = $e->getMessage();
}
?>




<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Database Connection with PDO</title>
    <link href="../../styles/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Connecting with PDO</h1>
<?php if(isset($error)) {
    echo "<p>$error</p>";
}else {

    ?>
<?php
if ( empty( $_POST ) ) {
    ?>



    <h1>Registration</h1>
    <form name="" action="" method="POST">
        <label for 'username'>Username: </label>
        <input type="text" name="fn"/>
        <label for 'password'>Password: </label>
        <input type="password" name="psw"/>
        <label for 'first_name'>First name: </label>
        <input type="text" name="fn"/>

        <br/>
        <button type="submit">Submit</button>
    </form>


<?php
} else {
    print_r( $_POST );
}
    ?>



<?php } //error  ?>

</body>
</html>

still doesnt do what i want ... doesnt add any data to the tables and AI users_id with last I I anyone can help with this ?

  • 写回答

1条回答 默认 最新

  • dsj0312 2014-12-19 14:13
    关注

    Short answer: RTM

    Long answer (from the mysql documentation) When a new AUTO_INCREMENT value has been generated, you can also obtain it by executing a SELECT LAST_INSERT_ID() statement

    You are not generating a new ID automatically, you are specifying the value directly. So you can do one of two things, remove the id column and value :id placeholder from your first query OR use the same :id placeholder in your second query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误