Data is not being stored in the database - php

My code is getting the ID from another, after I get that ID I will insert it to another table. The thing is it's not working, any idea why?
<?php
session_start();
include("Connection.php");
if (isset($_POST['submit'])){
$name = $_POST['customerName'];
mysql_query("INSERT INTO `starbucks`.`orders` (
`ID` ,
`NAME` ,
`TOTAL_PRICE` ,
`TOTAL_ITEMS` ,
`TIME`
)
VALUES (
'' , '$name', '', '',NOW())");
$_SESSION['user'] = $name;
}
$dTime = time();
$myValue = isset($_REQUEST['dValue']) ?$_REQUEST['dValue'] : '';
echo "The time is: {$dTime}<br/>
The choice is {$myValue} ";
$sql = "Select * from product where NAME = '{$myValue}'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
$price = $row['PRICE'];
$id = $row['ID'];
echo $id;
$sql2 ="INSERT INTO starbucks`.order_details (ID, ORDER_ID, PRODUCT_ID, QTY) VALUES ('', '', '$id', '1')";
$result2 = mysql_query($sql2);
}
?>

extra back tick in the INSERT, either add another or remove

Related

php - why the id automatically turn into 0?

it just set my id into 0 after i inserted some data into the table
then, I get this kind of error:
Duplicate entry '0' for key 'PRIMARY'.
can someone help me identify the problem?
$query = mysql_query("INSERT INTO customer VALUES('', '$name', '$phone', '$address', '$email_add')") or die(mysql_error());
$i=1;
foreach($_SESSION as $namee => $value)
{
if($value > 0)
{
if(substr($namee, 0, 5) == 'cart_')
{
$id = substr($namee, 5, (strlen($namee)-5));
$get = mysql_query("SELECT * FROM product WHERE code='$id'");
while($get_row = mysql_fetch_assoc($get)){
$sub = $get_row['price'] * $value;
echo '<p>'.$i.' '.$get_row['code'].' '.$get_row['name_nl'].' '.$value.' SubTotal : RM '.$sub.'</p> ';
$getCustomer = mysql_query("SELECT customer.id_customer, customer.name, customer.address, product.code, product.name_nl FROM customer, product WHERE name='$name' AND address='$address'" ) or die(mysql_error());
$data = mysql_fetch_array($getCustomer);
$pemb = $data['id_customer'];
$na = $data['name'];
$al = $data['address'];
$ib = $get_row['code'];
$nb = $get_row['name_nl'];
$i++;
}
}
mysql_query("INSERT INTO book VALUES('', '$pemb', '$na', '$al', '$ib', '$nb', '$value', '$sub', now()) ") or die(mysql_error());
}
}
Primary key fields must have different values.
To resolve this, you must set this field to AUTO_INCREMENT, so each time a new record is entered, the primary key field is incremented automatically.

Sending information from a newly created record to a different MySQL table

I'm making a form that submits a story into a MySQL table called 'work'. I want to later take the id of the newly created record and put the information into a different table.
But when I submit the story, it says:
$workid is undefined.
I can't see the problem though because I believe I've defined it?
<?php
if (!empty($_POST) && !empty($_POST['title']) && !empty($_POST['story']) && !empty($_POST['genre']) && !empty($_POST['rating'])) {
$title = strip_tags($_POST['title']);
$story = strip_tags($_POST['story']);
$title = mysqli_real_escape_string($db, $title);
$story = mysqli_real_escape_string($db, $story);
$genre = $_POST['genre'];
$rating = $_POST['rating'];
$query = "SELECT COUNT(*) AS count FROM works WHERE Title = '".$title."'";
$result = $db->query($query);
$data = $result->fetch_assoc();
if ($data['count'] > 0) {
echo "<p>Story already exists!</p>";
} else {
$query = "INSERT INTO works (author_id, login_id, Title, Story, Genre, Rating) VALUES ('".$userid."','".$authorid."','".$title."','".$story."','".$genre."','".$rating."')";
$query = "SELECT `id` FROM `works` WHERE `Title` = '".$title."'";
if ($result = $db->query($query)) {
while ($row = $result->fetch_assoc())
$workid = $row["id"]; //workid is written here but still considered undefined
}
$query = "INSERT INTO `author_work` (`author_id`) VALUES ('".$authorid."')";
$result = $db->query($query);
$query = "INSERT INTO `author_work` (`work_id`) VALUES ('".$workid."')";
$result = $db->query($query);
$query = "INSERT INTO `login_work` (`work_id`) VALUES ('".$workid."')";
$result = $db->query($query);
$query = "INSERT INTO `login_work` (`login_id`) VALUES ('".$userid."')";
$result = $db->query($query);
if ($result) {
echo "<p>Story submitted!</p>";
} else {
echo "SQL Error: " . $db->error;
}
}
}
?>
You never did a $db->query() on your INSERT INTO... query string, so it was never inserted, and was overwritten by your SELECT id ... query.
$query = "INSERT INTO works (author_id, login_id, Title, Story, Genre, Rating) VALUES ('".$userid."','".$authorid."','".$title."','".$story."','".$genre."','".$rating."')";
$db->query($query); // Missing this $db->query()
$query="SELECT `id` FROM `works` WHERE `Title` = '".$title."'";
if ($result = $db->query($query)) {
while ($row= $result->fetch_assoc())
$workid = $row["id"];}
Your $workid might not be initialized, depending on your condition and the result of your SQL query: so try to avoid next operations that will causes warnings/errors by using continue or else

how to get data from table and insert it into another table?

checkorder.php
<?php include("koneksi.php");mysql_select_db("project2");
$code = $_POST['code'];
$name = $sql = "Select name from 'orders' where 'code' = '<?php $_POST['code'] ?>' ";
$amount = $_POST['amount'];
$nameorder = $_POST['nameorder'];
$email = $_POST['email'];
$address= $_POST['address'];
$price = $sql= "Select price from 'orders' where 'code' = '<?php $_POST['code'] ?>' ";
$totalprice = $price * $_POST['amount'];
$sql = " INSERT INTO orders (idorder, date, nameorder, email, code, name, amount, price, total price)
VALUES
(default, NOW(), '$code', 'name','$amount','$nameorder','$email','$address')";
mysql_query($sql) ; ?>
I want to insert data using php.
code,name,price get from table product, is it possible using this way?
Because when I try to input data it always show error in $name.
Use query select at first then fetch what u want out
$code = $_POST['code'];
$query = "Select * from `orders` where `code` = '$code' ";
$result = mysql_query($query);
$name = mysql_fetch_assoc($result)['name']; //get the name
$amount = $_POST['amount'];
$nameorder = $_POST['nameorder'];
$email = $_POST['email'];
$address= $_POST['address'];.....................

Update multiple row using PHP and MySQL

I'm new to php and mySql. I'm trying to update multiple row by using php and mysql.
I'm having problem with updating multiple row in MySQL database. It's only update the last row of the table in the database. For example, user click on view product. The page will list 10 product that currently in the database. And user wants to update product information by on-click method. After finishing update, user click submit.
The problem is it only capture and update information of the last product in the table. I tried to put it in the foreach() function. But it doesnt work.
Please help. I just learned PHP and mySQL less than a week. I very much appreciate any helps.
<?php
include 'dbconn.inc.php';
include 'functions.inc.php';
$sql = "SELECT * FROM products";
$res = $mysqli->query($sql);
while( $row = $res->fetch_array(MYSQLI_ASSOC) ){
$products($row['id']) = 'id';
}
$id = $mysqli->real_escape_string( $_POST['id'] );
$weight = $mysqli->real_escape_string( $_POST['weight'] );
$name = $mysqli->real_escape_string( $_POST['name'] );
$supplier_id = $mysqli->real_escape_string( $_POST['supplier_id'] );
$price = $mysqli->real_escape_string( $_POST['price'] );
$description = $mysqli->real_escape_string( $_POST['description'] );
foreach( $products as $id){
$sql = "UPDATE products
SET
`id` = '$id',
`weight` = '$weight',
`price` = '$price',
`name` = '$name',
`supplier_id` = '$supplier_id',
`description` = '$description'
WHERE `id` = '$id'";
}
A couple of issues:
First, you're declaring the variable $id twice.
You should be using the $key not the $value in the loop
Instead, try this:
foreach( $products as $key => $value){
$sql = "UPDATE products
SET
`id` = '$id',
`weight` = '$weight',
`price` = '$price',
`name` = '$name',
`supplier_id` = '$supplier_id',
`description` = '$description'
WHERE `id` = '$key'";
}
The reason for using the array key rather than its value is because in the below line you are setting the key of the array to the values returned from the first query:
while( $row = $res->fetch_array(MYSQLI_ASSOC) ){
$products($row['id']) = 'id';
}
I might suggest instead doing this:
$products = array();
while( $row = $res->fetch_array(MYSQLI_ASSOC) ){
$products[]['id'] = $id;
}
foreach( $products as $product){
$sql = "UPDATE products
SET
`id` = '$id',
`weight` = '$weight',
`price` = '$price',
`name` = '$name',
`supplier_id` = '$supplier_id',
`description` = '$description'
WHERE `id` = '" . $product['id'] . "'";
}

Product ID is not being stored in the database

can anyone help? my code doesn't seem to store the value of product id here in my code have a look I am also getting the ID from another table
<?php
include("Connection.php");
$dTime = time();
$myValue = $_REQUEST['dValue'];
echo "<p>
The time is: {$dTime}<br/>
The choice is {$myValue}
</p>
";
$sql = "Select ID from product where NAME = '$myValue'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
$pid=$row["PRODUCT_ID"];
$sql2 = "INSERT INTO `starbucks`.`order_details` (
`ID` ,
`ORDER_ID` ,
`PRODUCT_ID` ,
`QTY`
)
VALUES (
NULL , '', '$pid', '1'
)";
$result2 = mysql_query($sql2);
?>
updated the code
$id = $row["ID"]
instead of:
$id = $row;
You have an incorrect array value for $id instead of the array's ID key:
$id = $row;
// Should be
$id = $row['ID'];
in your original code there is no error handling,you should do something like this:
$sql = "Select ID from product where NAME = '$myValue'";
if ($sql) {
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
$pid = $row["PRODUCT_ID"];
$sql2 = "INSERT INTO `starbucks`.`order_details` (
`ID` ,
`ORDER_ID` ,
`PRODUCT_ID` ,
`QTY`
)
VALUES (
NULL , '', '$pid', '1'
)";
$result2 = mysql_query($sql2);
if (!$result2) {
echo mysql_error();
break;
}
} else {
echo mysql_error();
}
And see what error you get.

Categories