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>
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.