dpr26232 2015-10-05 15:31
浏览 8

我想从一个表到另一个表获取值并插入一些表单值如何? [关闭]

I want to add some data to mysql table from another mysql table and at the same time i want to add some form $_POST values to the same table in the same row.

Note : IM NEW TO PHP AND MYSQLI

Basically im looking for the correct syntax, please do help me and thanks in advance.

<?php

if(isset($_POST['ask'])) {

require('includes/connection.php');

$problem = $_POST['problem'];
$expecting = $_POST['expecting'];

//SELECT * FROM users WHERE email = '$email' AND 

$string = "INSERT INTO ask(users_id,username,email,age,sex,problem,expecting) SELECT id,username,email,age,sex FROM users AND VALUES('$problem','$expecting') WHERE users.email = '$_SESSION[email]'";

$query = mysqli_query($dbc,$string);

if($query) {

    echo 'yes';

} else {

    echo 'something is wrong' . $query . mysqli_error();

}

}

?>


<!DOCTYPE html>

<html lang="en">

<head>

    <link rel="stylesheet" type="text/css" href="css/style.css">

</head>

<body>

    <div class="ask">

    <h1>Ask</h1>

    <form method="post" action="logged.php">

        <p><label>Problem: </label><input type="text" name="problem" value="<?php if(isset($problem)) { echo $problem; } ?>"></p>
        <p><label>Expecting: </label><input type="text" name="expecting" value="<?php if(isset($expecting)){ echo $expecting; } ?>"></p>
        <p><input type="submit" name="ask" value="Ask"></p>

    </form>

    </div>
    <form method="post" action="logged.php">

        <input type="submit" name="logout" value="Logout">

    </form>

</body>

</html>
  • 写回答

1条回答 默认 最新

  • dongxia8656 2015-10-05 15:36
    关注

    Your first problem is wrong database design. Instead of duplicating user's data in the questions table, you have only to link it. Read up on database normalization, and change your ask table by removing username,email,age,sex fields.

    Then get user_id from users table with SELECT query.

    Then run a conventional INSERT query

    INSERT INTO ask(users_id,problem,expecting)
    

    Note that you have to use prepared statements for database interactions.

    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备