I have the code below. I take with post some variables from another page. Then I run my query and I store them to the table materials. Then I select for this table in order to take materialsid and then I want with insert into to store the value of materialsid in another table called material_damages
<?php
session_start();
if(isset($_POST['submit'])) {
include('dbConfig.php');
$mname1=$_POST['name1'];
$mcost1=$_POST['cost1'];
$mquantity=$_POST['quantity'];
$res=mysql_query("INSERT INTO materials VALUES (NULL, '$mname1', '$mcost1','$mquantity')");
if ($res)
{
echo "Insert successful";
}
else
{
echo "Insert failed";
}
$res1=mysql_query("SELECT * FROM materials");
while ($row = mysql_fetch_array ($res1))
{
$id10=$row['materialsid'];
$id11=(int)$id10;
$res2=mysql_query("INSERT INTO damage_materials (damage_materials_id,damage_id,materials_id) VALUES (NULL,NULL,'$id11')");
if($res2)
{
echo "CORRECT";
}
else
{
echo "FALSE";
}
}
}
?>
The material is stored at table materials but the id does not get stored in the table damage_material. It prints Insert succesful FALSE FALSE FALSE FALSE (false is as the number of my materials)
Any ideas?
Related
I'm trying to insert data in two tables, with exploiting the the primary key of the first one and add it as foreign key in the second table,i think the problem is the two query are inserting each info in the same time and it give us a conflict between foreign key and primary one,any ideas ? maybe making something like a delay between each query !!
<?php
$InputCIN = filter_input(INPUT_POST,"CIN");
$InputID_PAYEMENT = filter_input(INPUT_POST,"ID_PAYEMENT");
$InputDATE_PAYEMENT = filter_input(INPUT_POST,"DATE_PAYEMENT");
$InputTYPE_DE_PAYEMENT = filter_input(INPUT_POST,"TYPE_DE_PAYEMENT");
$InputPRIX = filter_input(INPUT_POST,"PRIX");
$InputMOIS_PAYEMENT = filter_input(INPUT_POST,"MOIS_PAYEMENT");
$queryAj2_1 = "insert into payement values($InputID_PAYEMENT,'$InputDATE_PAYEMENT','$InputTYPE_DE_PAYEMENT',$InputPRIX,'$InputMOIS_PAYEMENT')";
$queryAj2_2 = "insert into payer values('','$InputCIN',$InputID_PAYEMENT)";
if(isset($_POST['butAj2']))//submit button
{
if($InputID_PAYEMENT && $InputCIN)
{
if(mysqli_query($con,$queryAj2_1) && mysqli_query($con,$queryAj2_2))
{
$msg = "<div class='alert alert-success'><span class='fa fa-square-check'></span>Adding successfull</div>";
}
else
{
$msg = "<div class='alert alert-danger'><span class='fa fa-square-check'></span>Erorr adding</div>".mysqli_error($con);
}
}
else if(!$InputID_PAYEMENT && !$InputCIN)
{
$msg = "<div class='alert alert-danger'><span class='fa fa-square-check'></span>ADD CIN/ID PAYEMENT</div>".mysqli_error($con);
}
else{
$msg = "<div class='alert alert-danger'><span class='fa fa-square-check'></span>erorrrrrrrrrrrrr</div>".mysqli_error($con);
}//it show me this msg
}
>
The mysqli_multi_query() function performs one or more queries against the database.
The queries must be separated with a semicolon.
from php.net
MySQL optionally allows having multiple statements in one statement
string. Sending multiple statements at once reduces client-server
round trips but requires special handling.
Multiple statements or multi queries must be executed with
mysqli_multi_query(). The individual statements of the statement
string are separated by semicolon. Then, all result sets returned by
the executed statements must be fetched.
The MySQL server allows having statements that do return result sets
and statements that do not return result sets in one multiple
statement.
$queryAj2_1 = "insert into payement values($InputID_PAYEMENT,'$InputDATE_PAYEMENT','$InputTYPE_DE_PAYEMENT',$InputPRIX,'$InputMOIS_PAYEMENT');";
$queryAj2_2 = "insert into payer values('','$InputCIN',$InputID_PAYEMENT)";
// Execute multi query
if (mysqli_multi_query($con, $queryAj2_1. $queryAj2_2))
{
// print message on success
Use of the multiple statement with prepared statements is not supported.
There is been conflict when you are trying to execute both the query at the same time. The 2nd table does not able to take the reference of the 1st tables primary key.
Try this out....
if(mysqli_query($con,$queryAj2_1))
{
if(mysqli_query($con,$queryAj2_2))
{
$msg = "<div class='alert alert-success'><span class='fa fa-square-check'></span>Adding successfull</div>";
}
}
So, here there will be no chance of conflict and primary key will be inserted first into the table and then it's foreign key.
You can Try the code below to fire multiple query in PHP.
$sql = "INSERT INTO requestdetails (`userid`, `dayte`) VALUES ('$userid','$date');INSERT INTO managerrepo (`userid`, `dayte`) VALUES ('$userid', '$date') ";
if($conn->multi_query($sql) === TRUE)
{
echo "You have requested successfully!";
unset($_POST);
//header("location: index.php?id=".$id);
}
else
{
echo "Problem in request. Try Again!";
}
OR
you use the below code too, for insertion in same table:
$sql = "INSERT INTO requestdetails (`userid`, `dayte`) VALUES ('$userid','$date')";
$sql_new = "INSERT INTO requestdetails (`userid`, `dayte`) VALUES ('$userid', '$date') ";
if($conn->multi_query($sql) === TRUE)
{
if($conn->multi_query($sql_new) === TRUE)
{
}
else
{
echo "error";
}
}
else
{
echo "Problem in request. Try Again!";
}
Hello iam trying to create a disciplinary system. i created a table where accused and the victim had to put their information. I have created another table for report and i have failed to combine all the information given in the first table into one doc file so that i can use it in report. I need help?
enter code here
if(isset($_POST['call'])){
$accused = $_POST['dss'];
$report= $_POST['cas'];
$action = $_POST['uect'];
$u_cat = intval($_POST[id]);
if($report ==''){
echo "<font color='Green'><b>Please fill in the Report!</b></font>";
}else{
if($u_cat) $insert = "update discip_report set accused_student='$accused', case='$report', action_taken='$action' where id='$u_cat'";
else $insert = "insert into discip_report(accused_student, report, action_taken, date) values('$accused','$report', '$action', NOW())";
$run = mysql_query($insert);
if ($run) {
echo "<font color='Green'><b>The Category was added</b></font>";
}else{
echo "<font color='Green'><b>The Category was not added</b></font>";
}
}
}
?>
I'm trying to insert in each row of database the variable $num which is an Array. I want to insert each array position in each row of database.
That's the code I have, and in the moment it is inserting the word "Array":
foreach($html2->find('small[class=menu-item__label__count]') as $aholder) {
$holderdes=$aholder->outertext;
}
preg_match_all('!\d+!', $holderdes, $num);
//preg_match_all('/(\([0-9]+)\)/', $holderdes,$num);
echo print_r($num);
echo("<script>console.log('PHP: ".$num."');</script>");
$insert="INSERT into spec_insert(designers) VALUES ('".$num."');";
$sql=mysql_query($insert);
if ($sql === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " ;
}
}
If you want to insert each array value to a specific column of DB table spec_insert then can use foreach() to retrieve the index value of array. Example
foreach($num as $val):
if(is_array($val)):
foreach($val as $v):
$insert="INSERT into spec_insert(designers) VALUES ('".$v."');";
$sql=mysql_query($insert);
endforeach;
else:
$insert="INSERT into spec_insert(designers) VALUES ('".$val."');";
$sql=mysql_query($insert);
endif;
endforeach;
I want to fetch and display the user_id(that is auto-incremented) from my table after a user is successfully registered and his user information are stored in the same table. Here goes my code. I need help on the last few lines.
if(isset($_POST['register']))
{
$fname=$_POST['fname'];
$mname=$_POST['mname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$pwd=$_POST['pwd'];
$address=$_POST['address'];
$pincode=$_POST['pincode'];
$phone=$_POST['phone'];
$sql="INSERT INTO `user`(`f_name`,`m_name`,`l_name`,`email`,`password`,`address`,`pincode`,`phone`) VALUES('$fname','$mname','$lname','$email','$pwd','$address','$pincode','$phone')";
$rs=mysql_query($sql);
if($rs==1)
{
echo 'You have been registered successfully!';
}
else
{
echo "Registration failed!";
}
}
$src="SELECT `user_id` from `user` where `email`=$email";
$res=mysql_query($src);
$row=mysql_fetch_field($res);
echo $row;
echo $row[0];
$row is an array of fields, even if in your case it contains only one element. For example, if you use
SELECT a, b, c FROM ...
then $row is an array of 3 elements, $row[0] being for a, $row[1] for b, etc.
mysql_fetch_field — Get column information from a result and return as an object.
$row=mysql_fetch_field($res,0);
if(!row)
{
echo "There is no record";
}
else
{
echo $row->user_id;
}
Check Manual here
In a mysql table, i have 3 fields. user_to, user_from and id. The variables are all correct and should be inserting the correct data.
When a button is clicked, named 'poke', it should insert the cookie that stores the session of who did it and the person who was poked. It doesn't seem to be inserting and I am stuck :(
$cookie = $_SESSION['user_login'];
//Poke code
if (#$_POST['poke']) {
$check_if_poked = mysql_query("SELECT * FROM pokes WHERE user_to='$username' && user_from='$added_by'");
$num_poke_found = mysql_num_rows($check_if_poked);
if ($num_poke_found == 1) {
echo "Come on! Give the guy a chance!";
}
else
if ($username == $cookie) {
echo "You cannot Jab yourself.";
}
else
{ $poke_user = mysql_query("INSERT INTO `pokes` ('user_from', 'user_to') VALUES ('$cookie', '$username')") or trigger_error(mysql_error());
echo "$username has been jabbed.";
}
}
You used wrong quotes with fields in MySQL query.
//your wrong variant
"INSERT INTO `pokes` ('user_from', 'user_to') VALUES ('$cookie', '$username')"
//right variant
"INSERT INTO `pokes` (`user_from`, `user_to`) VALUES ('$cookie', '$username')"
Quotes like ' mean values and quotes like ` mean fields in SQL syntax
<?php
if ($_POST['poke']) {
#ref to the current user
$from = $_SESSION['user_login'];
#ref to the (poked user)
$to = $_POST['poked_user_id'];
if($from == $to){
echo "you cant poke yourself!";
}
else{
#ADVICE: USE PDO OR MYSQLI INSTEAD OF MYSQL
$check_if_poked = mysql_query("SELECT * FROM pokes WHERE user_to='$to' AND user_from='$from'");
if(mysql_num_rows($check_if_poked)){
echo "Come on! Give the guy a chance!";
}
else{
if(mysql_query("INSERT INTO `pokes` (`user_from`, `user_to`) VALUES ('$from', '$to')")){
echo "$to has been jabbed.";
}
else{
trigger_error(mysql_error());
}
}
}
}
?>
This started off as a comment - but it's getting too long to fit.
A session is not the same thing as a username - your post is very confused.
Leaving aside the wrong quotes (which is why your code is not doing what you expect)....
In a mysql table, i have 3 fields. user_to, user_from and id
... in that case you don't need to check if the row already exists - and not create duplicates. Set up a unique index then...
if (#$_POST['poke'] && ($_SESSION['user_login']!===$username)) {
$qry = "INSERT INTO `pokes` (`user_from`, `user_to`)
VALUES (".mysql_real_escape_string($_SESSION['user_login'])
.", '" . mysql_real_escape_string($username) . "')"
if (mysql_query($qry)){
echo "$username has been jabbed.";
} else if (stristr(mysql_error(), 'duplicate')) {
echo "Come on! Give the guy a chance!";
} else {
echo "It's all gone Pete Tong!";
}
} else if ($_SESSION['user_login']!===$username) {
echo "You cannot Jab yourself.";
}
While it's about the same effort for PHP processing, the DB workload is significantly less. This code also prevents some SQL injection attacks, and has error handling. I presume that $username has been created elsewhere and you don't have register_globals enabled.