PHP MySQL Insert not working without error - php

i used this code and i didnt get any errors but when i check the phpadmin and my database its all empty no data added to the table what should i do ?
where is the problem?
i checked also my database and table values and my html names
<?php
if(isset( $_POST['submit'] ) )
{
$name = $_POST['p_name'];
$price = $_POST['p_price'];
$type = $_POST['p_type'];
$des = $_POST['p_desc'];
$img = 'images/is7.jpeg';
//***********************
require_once 'setting.php';
$dbc = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
mysqli_set_charset($dbc, 'utf8');
$query = "INSERT INTO products(name, price, type, des, img)
VALUES('$name', '$price', '$type', $des, '$img')";
mysqli_query($dbc, $query);
mysqli_close($dbc);
header('Location: success.php');
exit;
}
?>

Did you try to put single quotes around description variable?
Change
VALUES('$name', '$price', '$type', $des, '$img')";
To
VALUES('$name', '$price', '$type', '$des', '$img')";
Cheers

"INSERT INTO products(name, price, type, des, img) VALUES('$name', '$price', '$type', '$des', '$img')";
OR
"INSERT INTO products(name, price, type, des, img) VALUES(?, ?, ?, ?, ?)";

Replare this formate
<?php
$query = "INSERT INTO products(name, price, type, des, img)VALUES('$name', '$price', '$type', $des, '$img')";
?>
TO
<?php
$query = "INSERT INTO products(name, price, type, des, img)VALUES('".$name."', '".$price."', '".$type."', '".$des."', '".$img."')";
?>
In feature apply mySQL error check up to get mySQL error.

Related

How to use php variables as table name to insert records in mysql

$id = $_SESSION['id'];
i have the table name stored in the $id variable.
when i use the variable name with sql query it doesn't work
$sql = "INSERT INTO karthick.$id (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('$name', '$tin', '$address', '$product', '$invoice', '$transport', '$cutting', '$amount', '$vat')";
when i replace the karthick.$id as karthick.ford it works fine. but i want to use the variable stored in $id as my table name. how do i do it.
Edit---------------------------
my php code
<?php
session_start();
$id = $_SESSION['id'];
require 'database.php';
/*$sql = "CREATE TABLE karthick.details (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL)";
if($conn->query($sql)===TRUE){
echo "table created";
}else{
echo "table not created";
}*/
$name = mysqli_real_escape_string($conn, $_POST["cname"]);
$tin = mysqli_real_escape_string($conn, $_POST["tin"]);
$address = mysqli_real_escape_string($conn, $_POST["address"]);
$product = mysqli_real_escape_string($conn, $_POST["product"]);
$ddate = mysqli_real_escape_string($conn, $_POST["date"]);
$invoice = mysqli_real_escape_string($conn, $_POST["invoice"]);
$transport = mysqli_real_escape_string($conn, $_POST["transport"]);
$cutting = mysqli_real_escape_string($conn, $_POST["date"]);
$amount = mysqli_real_escape_string($conn, $_POST["amount"]);
$vat = mysqli_real_escape_string($conn, $_POST["vat"]);
$val = 'karthick'.$id;
echo $val;
$sql = "INSERT INTO $val (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('$name', '$tin', '$address', '$product', '$invoice', '$transport', '$cutting', '$amount', '$vat')";
if($conn->query($sql)===TRUE){
echo "record inserted";
}else{
echo "not inserted".$conn->error;
}
$conn->close();
?>
Try this one.
$val = 'karthick'.$id;
$sql = "INSERT INTO $val (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('$name', '$tin', '$address', '$product', '$invoice', '$transport', '$cutting', '$amount', '$vat')";
Try using "INSERT INTO karthick.".$id." (name, tin, address, product, invoice, transport
Place the $id in braces{} like karthick.{$id}. The query should be like below.
$sql = "INSERT INTO karthick.{$id} (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('$name', '$tin', '$address', '$product', '$invoice', '$transport', '$cutting', '$amount', '$vat')";
$val = 'karthick'.$id;
$val = str_replace(" ","",$val);
$sql = "INSERT INTO $val (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('$name', '$tin', '$address', '$product', '$invoice', '$transport', '$cutting', '$amount', '$vat')";
Try
$sql = "INSERT INTO karthick.{$id} (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('{$name}', '{$tin}', '{$address}', '{$product}', '{$invoice}', '{$transport}', '{$cutting}', '{$amount}', '{$vat}')";
You are creating karthick.details table instead of karthick.session_id
First method :
Try putting the PHP variables inside curly braces {}
Like:
$sql = "INSERT INTO karthick.{$id} (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('{$name}', '{$tin}', '{$address}', '{$product}', '{$invoice}', '{$transport}', '{$cutting}', '{$amount}', '{$vat}')";
Second method :
Use PHP variables outside quotes
Like:
$sql = " INSERT INTO karthick.".$id." (name, tin, address .....
Update
try using `` to enclose your table like,
$val = "`karthick`.`".$id."`";

Issue with MySQLI

I'm probably just too tired, and looking over a small error, but this code will not input any information into my database.
$sql = "INSERT INTO TABLE_NAME (UPC, Description, Make, Model, SNLocation, IMEI_MEID, Resetting, Notes, Image)
VALUES ($UPC, $Desc, $Make, $Model, $SNLocation, $IMEI_MEID, $Resetting, $Notes, $Image)";
mysqli_query($con, $sql) or die(mysqli_error($sql));
Could someone help me check if this has syntax issues or something?
Its because you had an error
$sql = "INSERT INTO TABLE_NAME (UPC, Description, Make, Model, SNLocation, IMEI_MEID, Resetting, Notes, Image)
VALUES ('$UPC', '$Desc', '$Make', '$Model', '$SNLocation', '$IMEI_MEID', '$Resetting', '$Notes', '$Image')";
removed ] from Notes, Image)] <----- and values need to be quoted
You need to remove ] and enclose string values with single quotes(') :
$sql = "INSERT INTO TABLE_NAME
(UPC, Description, Make, Model,
SNLocation, IMEI_MEID, Resetting, Notes, Image)] <-- Here -->
VALUES ($UPC, $Desc, $Make, $Model, $SNLocation, $IMEI_MEID, $Resetting, $Notes, $Image)";
mysqli_query($con, $sql) or die(mysqli_error($sql));

how to insert into two tables using php with transaction?

I know inserting into multiple tables with single query is not possible,
now I am trying to insert into 2 tables with php using START TRANSACTION but its not working.
my sql query is looks like
mysqli_query($con,"START TRANSACTION
INSERT INTO users VALUES ('', '$getuser', '$getpass', '$getemail', '$fname', '$lname', '$domain', '$address1', '$address2', '$city', '$country', '$region', '$zip', '$phone', '$getplan', '$duration', '$getprice', '', '0', '0', '$code', '$date', '$time','0', '', '')
INSERT INTO domains (username) VALUES ('$getuser') COMMIT");
So where is the problem??
many thanks in advance.
it is because mysqli_query can handle only one comand each time. Split them like:
mysqli_query($con, "SET AUTOCOMMIT=0");
mysqli_query($con,"START TRANSACTION");
$insert1 = mysqli_query($con,"INSERT INTO users VALUES ('', '$getuser', '$getpass', '$getemail', '$fname', '$lname', '$domain', '$address1', '$address2', '$city', '$country', '$region', '$zip', '$phone', '$getplan', '$duration', '$getprice', '', '0', '0', '$code', '$date', '$time','0', '', '')");
$insert2 = mysqli_query($con,"INSERT INTO domains (username) VALUES ('$getuser')");
if($insert1 && $insert2) {
mysqli_query($con,"COMMIT");
} else {
mysqli_query($con,"ROLLBACK");
}
mysqli_query($con, "SET AUTOCOMMIT=1");
update: if you are using mysqli the objectorientated way, you can do the following:
$mysqli->begin_transaction();
$insert1 = $mysqli->query("INSERT INTO users VALUES ('', '$getuser', '$getpass', '$getemail', '$fname', '$lname', '$domain', '$address1', '$address2', '$city', '$country', '$region', '$zip', '$phone', '$getplan', '$duration', '$getprice', '', '0', '0', '$code', '$date', '$time','0', '', '')");
$insert2 = $mysqli->query("INSERT INTO domains (username) VALUES ('$getuser')");
if($insert1 && $insert2) {
$mysqli->commit();
} else {
$mysqli->rollback();
}
HINT: if you use an older PHP version as 5.5.0, you can't use $mysqli->begin_transaction(); and have to use $mysqli->autocommit(false); at the begining and $mysqli->autocommit(true); at the end instead
Instead of
if($insert1 && $insert2)
name it just "insert" and then:
if ($insert->affected_rows > 0)

PHP Mysql Query (INSERT) Issue

I am having an issue with a MySQL query as follows:
My script generates this as an example query:
INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('Test2', '123-456-7890', 'test#test.com', 'mesa', 'az', '04-14-2013')
Which if I drop directly into PHPMyA, works fine. However, the PHP script I am trying to use to send the query from my website is not working and I can't get it figured out. Here it is:
$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";
mysql_query($sql);
$result = mysql_query($sql);
if($result)
{
echo("<br>Data Input OK");
}
else
{
echo("<br>Data Input Failed");
}
Nothing makes it to the MySQL DB and no PHP errors are displayed, however, if I echo $sql I get the exact query I posted previously.
Just remove the single line mysql_query($sql); on your code and you will be fine.. But you should better start practicing PHP MySQLi which stands for PHP MySQL Improved, such:
$con = mysqli_connect($host, $user, $password, $password);
$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";
$result = mysqli_query($con, $sql);
if($result) {
echo("<br>Data Input OK");
} else {
echo("<br>Data Input Failed");
}
$sql = 'INSERT INTO Table_name (`id`, `name`) VALUES ("1", "php");
You are executing $sql twice in your script wich is causing the error, please remove
mysql_query($sql);
And it will be ready to go
I would also suggest to stop using mysql_query please switch to mysqli or PDO
Are you sure there is a valid connection (..mysql_connect())? Try using the full syntax like so..
$conn = mysql_connect(...);
$result = mysql_query($query, $conn);
Also try forcing a commit after you execute the statement -
$mysql_query("COMMIT", $conn);
You are running mysql_query twice. Reason of the error. Try running the following code.
$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";
$result = mysql_query($sql) or die(mysql_error());
if($result){
echo("<br>Data Input OK");
} else{
echo("<br>Data Input Failed");
}
use this
"INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$_POST[name]', '$_POST[phone]', '$_POST[email]', '$_POST[city]', '$_POST[state]', '$_POST[date]')";
Try to use mysql_query($sql,$con); instead of mysql_query($sql);.
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$age=$_POST['age'];
$address=$_POST['address'];
$ins="insert into table_name(`name`,`age`,`address`)values('".$name."','".$age."','".$address."')";
mysql_query($ins);
echo 'data inserted successfully';
}

Activate headers after php request is complete

I am currently working on a site where users can upload images up to 10mb. When the users press upload, the file is stored in a directory on the server, and then the page is redirected through the use of a header. Note that I also put ob start() at the beginning of the script. The problem I'm having is that the header redirect activates before the request is complete, so users get logged out, or an internal server error occurs. Same thing happens when you delete the large file. I've tried removing the redirect and its fine, so i'm sure that's the problem. Is there a way to get the header redirect only after the php request (image upload) is complete?
Thanks!
Edit: Code sample
if (empty($errors)) {
// Get the filename minus the file extension:
$filename = substr($image["name"], 0, strrpos($image["name"], "."));
// Append the appropriate extension
$filename .= $validMimes[$image['type']];
$location = "/home/shoplft/users/$user_id/$foldername/$filename";
move_uploaded_file($tmp_name, $location);
if($secondcategory=="0") {
mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store, website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());
$pic_id = mysql_insert_id();
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");
if(!empty($price)) {
mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");
}
}
else {
mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store, website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());
$pic_id = mysql_insert_id();
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$secondcategory')");
if(!empty($price)) {
mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");
}
}
header("Location: product.php?id=$pic_id");//prolly should separate data processing from output
}
Try this
if (empty($errors)) {
// Get the filename minus the file extension:
$filename = substr($image["name"], 0, strrpos($image["name"], "."));
// Append the appropriate extension
$filename .= $validMimes[$image['type']];
$location = "/home/shoplft/users/$user_id/$foldername/$filename";
if(!#copy($tmp_name, $location))
{
$errors[] = 'Error';
}
if (empty($errors))
{
if($secondcategory=="0") {
mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store, website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());
$pic_id = mysql_insert_id();
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");
if(!empty($price)) {
mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");
}
}
else {
mysql_query("INSERT INTO products (users_user_id, name, brand, city, country, store, website, price, keywords, productlocation, filename) VALUES ('$user_id', '$productname', '$productbrand', '$city', '$country', '$store', '$website', '$price', '$tagwords', '$location', '$filename')") or die('Invalid query: ' . mysql_error());
$pic_id = mysql_insert_id();
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$firstcategory')");
mysql_query("INSERT INTO categories (products_products_id, products_users_user_id, category) VALUES('$pic_id', '$user_id', '$secondcategory')");
if(!empty($price)) {
mysql_query("INSERT INTO svalues (products_products_id, products_users_user_id, svalue) VALUES ('$pic_id', '$user_id', '$price') ");
}
}
header("Location: product.php?id=$pic_id");//prolly should separate data processing from output
}
}

Categories