Couldn't enter the data to mysql using php - php

It couldn't store the data to mysql. What to do? All variable and file name are correct.
<?php
require 'connection.php';
$conn = Connect();
$id =$conn->real_escape_string ($_POST['id']);
$name = $conn->real_escape_string ($_POST['name']);
$phone = $conn->real_escape_string ($_POST['phone']);
$address = $conn->real_escape_string ($_POST['address']);
$city = $conn->real_escape_string ($_POST['city']);
$zip = $conn->real_escape_string ($_POST['zip']);
$state = $conn->real_escape_string ($_POST['state']);
$item = $conn->real_escape_string ($_POST['item']);
$status = $conn->real_escape_string ($_POST['status']);
$enquiry_date = $conn->real_escape_string ($_POST['enquiry_date']);
$enquiry_user = $conn->real_escape_string ($_POST['enquiry_user']);
$query = "INSERT into enquiry
(id, name, phone, address, city, zip, state, item, status, enquiry_date, enquiry_user)
VALUES('" . $id . "','" . $name . "','" . $phone . "','" . $address . "','" . $city . "','" . $zip . "','" . $state . "','" . $item . "','" . $status . "','" . $enquiry_date . "')";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
echo "Thank You For Contacting Us <br>";
$conn->close();
?>

As #Jeff said:
$query = "INSERT into enquiry
(id, name, phone, address, city,
zip, state, item, status, enquiry_date, enquiry_user)
VALUES('" . $id . "','" . $name . "','" . $phone . "','" . $address . "','"
. $city . "','" . $zip . "','" . $state . "','" . $item . "','"
$status . "','" . $enquiry_date . "','" . $enquiry_user . "')";
You were missing . "','" . $enquiry_user

Related

How to set charset in import CSV file to mysql database?

I wrote a code that imports data from a CSV file into the database. However, I have a problem with setting charset on cp1250. When I use Polish characters in the database I see strange characters. I tried to use the SET NAMES function, but it does not work - any ideas?
<?php
$conn = mysqli_connect("localhost", "b", "c", "d");
if (isset($_POST["import"])) {
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {
$sqlInsert = "INSERT INTO evdb_Historia_Aktualizacja(ID,Imie_I_Nazwisko,Suma_Brutto_Przejazdy,Suma_Netto_Przejazdy,Gotowka_Przejazdy_Uber,Brutto_UberEats,Netto_UberEats,VAT_UberEats,Netto_Gotowka,Bonusy_Dodatki,Napiwki_Brutto,Napiwek_18,Napiwek,Dodatki_UberEats,Ilosc_Dni,Prowizja_Evelstar,Kwota_Po_Prowizji_Evelstar,VAT_Przejazdy,Faktury_Brutto,Faktury_Netto,VAT_Faktury,VAT_Nalezny,Suma_Bez_Skladek,Rodzaj_Umowy,Kwota_Skladek,Potracenia_Reczne,Potracenia_Stale,Przelew_Przewoz,Przelew_UberEats,Data_Od,Data_Do,Login)
values ('" . $column[0] . "','" . $column[1] . "','" . $column[2] . "','" . $column[3] . "','" . $column[4] . "','" . $column[5] . "','" . $column[6] . "','" . $column[7] . "','" . $column[8] . "','" . $column[9] . "','" . $column[10] . "','" . $column[11] . "','" . $column[12] . "','" . $column[13] . "','" . $column[14] . "','" . $column[15] . "','" . $column[16] . "','" . $column[17] . "','" . $column[18] . "','" . $column[19] . "','" . $column[20] . "','" . $column[21] . "','" . $column[22] . "','" . $column[23] . "','" . $column[24] . "','" . $column[25] . "','" . $column[26] . "','" . $column[27] . "','" . $column[28] . "','" . $column[29] . "','" . $column[30] . "','" . $column[31] . "')";
$result = mysqli_query($conn, $sqlInsert);
if (! empty($result)) {
$type = "success";
$message = "CSV Data Imported into the Database";
} else {
$type = "error";
$message = "Problem in Importing CSV Data";
}
}
}
}
?>

I am unable to increament id

I searched many stackoverflow questions it didn't help
I want to increment id by fetching last id from MySQL table.
I don't want to do auto increment in MySQL table because already one column is auto incremented.
<?php
include 'db.php';
$created = date('Y-m-d H:i:s');
//$json_data = array();
$message = array();
$error = array();
if ($_GET['vendor_id'] == "") {
$message[] = array("message" => "Values Empty");
} else {
$result = mysqli_query("SELECT loo_id FROM loo_list ORDER BY loo_id DESC LIMIT 1");
if ($result) {
$order_array = mysqli_fetch_assoc($result) or die(mysqli_error());
//echo $order_array['loo_id'];
}
$loo_id = $order_array['loo_id'] + 1;
$sql = "insert into loo_list(loo_id,name,address,geolocation,price,facility_category,facilities,count,accessbility,image,type,category,created_vendor,days,timings,terms_conditions,vendor_approval,created,warning,url,user_ids,overall,admin_approval,updated)values('" . $loo_id . "','" . $_GET['loo_name'] . "','" . $_GET['address'] . "','" . $_GET['loo_location'] . "','" . $_GET['price'] . "','" . $_GET['facility_category'] . "','" . $_GET['facilities'] . "','" . $_GET['count'] . "','" . $_GET['accessbility'] . "','" . $_GET['image'] . "','Offerers','" . $_GET['category'] . "','" . $_GET['vendor_id'] . "','" . $_GET['days'] . "','" . $_GET['timings'] . "','" . $_GET['terms_conditions'] . "','1','" . $created . "','0','','" . $_GET['user_ids'] . "','" . $_GET['overall'] . "','1','" . $created . "')";
$res1 = mysqli_query($db, $sql) or die(mysqli_error());
$message[] = array("message" => "success");
}
$json_data = array("result" => $message);
echo json_encode($json_data);
?>
Try this code.
if(trim($order_array['loo_id']) === ''){
$loo_id = 1;
}else{
$loo_id = intval($order_array['loo_id']) + 1;
}

PHP MySQL INSERT query not inserting all values

I have this variables :
$cliente = mysqli_real_escape_string($conn, htmlentities($_GET["cliente"]));
$metatickt = mysqli_real_escape_string($conn, htmlentities($_GET["metatickt"]));
$metaext = mysqli_real_escape_string($conn, htmlentities($_GET["metaext"]));
$metaenc = mysqli_real_escape_string($conn, htmlentities($_GET["metaenc"]));
$uf = mysqli_real_escape_string($conn, htmlentities($_GET["estado"]));
And I'm trying to do this query :
$queryInst = "INSERT meta_control (mes,cliente,meta_exit,meta_tickt,meta_enc,uf)"
. " VALUES (MONTH(NOW()),'" . $cliente . "','" . $metaext . "','" . $metatickt . "','" . $metaenc . "','" . $uf . "')";
mysqli_query($conn, $queryInst);
But when I check my db the only column that is not NULL is mes. What could be the cause to this?
You should have to use proper query. INSERT INTO
$queryInst = "INSERT INTO meta_control (mes,cliente,meta_exit,meta_tickt,meta_enc,uf)"
. " VALUES (MONTH(NOW()),'" . $cliente . "','" . $metaext . "','" . $metatickt . "','" . $metaenc . "','" . $uf . "')";
mysqli_query($conn, $queryInst);
You are probably getting an SQL error.
You should check for such errors using mysqli_error($conn) - http://php.net/manual/en/mysqli.error.php. In this case you probably have an error caused by the absence of the INTO keyword in your SQL query, which should be INSERT INTO ....
http://dev.mysql.com/doc/refman/5.7/en/insert.html

Query runs but does not insert data to the db

i am trying to insert details to the db. it runs a success message but doesnt insert any data to the db. tried different ways but still no data is inserted to the db.
i have updated my code to show the whole process.
code:
<?php
$con = mysqli_connect("localhost","root","","the_official_one");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit'])){
$prod_name = strtoupper($_POST['prod_name']);
$prod_brand = $_POST['prod_brand'];
$prod_cat = $_POST['prod_model'];
$prod_price = $_POST['prod_price'];
$prod_desc = $_POST['prod_desc'];
$prod_qty = $_POST['prod_qty'];
$d = date("Y-m-d");
mysqli_query($con,"INSERT into products(product_cat, product_brand, product_title, product_price, product_desc, product_qty, product_image, date)
VALUES ('" . $prod_cat . "','" . $prod_brand . "','" . $prod_name . "','" .$prod_price . "','" . $prod_desc . "','" . $prod_qty . "', '" . $prod_name . "', '" . $d . "')");
$uniq_id = mysqli_insert_id($con);
echo $uniq_id." ";
foreach($_FILES['product_image']['tmp_name'] as $key => $tmp_name){
$name = $_FILES['product_image']['name'][$key];
$tmpname = $_FILES['product_image']['tmp_name'][$key];
$type = $_FILES['product_image']['type'][$key];
$size = $_FILES['product_image']['size'][$key];
mkdir("product_images/$prod_name/");
$dir = "product_images/$prod_name/";
$move = move_uploaded_file($tmpname, $dir.$name);
$path = "$prod_name/".$_FILES['product_image']['name'][$key];
$img = $_FILES['product_image']['name'][$key];
if($move) {
$sql = mysqli_query($con,"INSERT into product_images(prod_id, image) VALUES ('".$uniq_id."', '".$img."')");
$query = mysqli_query($con,"update products set product_image_1='$path' where product_title='$prod_name'");
if($query) {
header('location: add_prods.php?message=success');
} else {
header('location: add_prods.php?message=failed');
}
} else {
echo '<hr>Picture upload failed<br /><hr />';
}
}
}
?>
the code with error is:
mysqli_query($con,"INSERT into products(product_cat, product_brand, product_title, product_price, product_desc, product_qty, product_image, date)
VALUES ('" . $prod_cat . "','" . $prod_brand . "','" . $prod_name . "','" .$prod_price . "','" . $prod_desc . "','" . $prod_qty . "', '" . $prod_name . "', '" . $d . "')");
mysqli_autocommit($con,TRUE);
mysqli_query($con,"INSERT into products(product_cat, product_brand, product_title, product_price, product_desc, product_qty, product_image, date)
VALUES ('" . $prod_cat . "','" . $prod_brand . "','" . $prod_name . "','" .$prod_price . "','" . $prod_desc . "','" . $prod_qty . "', '" . $prod_name . "', '" . $d . "')");
OR
mysqli_commit($con);
Either way you should think about closing the connection when you are done.
mysqli_close($con);
if neither of those work, try outputting the content of
mysqli_query($con,"INSERT into products(product_cat, product_brand, product_title, product_price, product_desc, product_qty, product_image, date)
VALUES ('" . $prod_cat . "','" . $prod_brand . "','" . $prod_name . "','" .$prod_price . "','" . $prod_desc . "','" . $prod_qty . "', '" . $prod_name . "', '" . $d . "')"
to see what is going wrong.
Either way Escape the variables if you havent before building that query to prevent SQL Injection.

parse csv and input to mysql

while(($data = fgetcsv($handle,0,",")) !== FALSE){
$num = count($data);
$sql = "INSERT INTO `calendar` (`service_id`, `sunday`, `monday`, `tuesday`, `wednesday`, `thursday`, `friday`, `saturday`, `start_date`, `end_date`) VALUES ('" . $data[0] . "','" . $data[1] . "','" . $data[2] . "','" . $data[3] . "','" . $data[4] . "','" . $data[5] . "','" . $data[6] . "','" . $data[7] . "','" . $data[8] . "','" . $data[9] . "');";
$collect .= $sql;
$count = count(explode(";",$collect));
if($count > 500){
$mysql->multi_query($collect);
$collect = '';
$count = 0;
}
$row++;
}
i'm parsing a csv and collecting queries and if it's more then 500 submitting to sql server
but the multi_query run only once why?

Categories