Error: Unknown column in 'where clause' - php

I have a form that is populated with mySQL db values upon loading the page. The data in the form can be edited and then a submit button that send the data to mysql to overwrite the values that were preloaded in the form. The fields are displayed in the form correctly but when changed and submitted I receive the following:
Error:Unkown column in 'where clause'.
I'm relatively new to working with mysql query's instead of taking the 'phpmyadmin' route ._.
Here's the query I used to fill the table with the original form data:
INSERT INTO mytable VALUES ("sunday", "name1", "price1"), ("monday", "name2", "$35"), ("tuesday", "name3", "$100"), ("wednesday", "name4", "$65"), ("thursday", "name5", "$5"), ("friday", "name6", "$57"), ("saturday", "name7", "$10");
Here's the sql query used to populate the form and then assigned to a variable by usingfetch_array
$sun_data = mysql_query("SELECT * FROM mytable WHERE day='sunday'")
or die(mysql_error());
... ...
$sun_info = mysql_fetch_array( $sun_data );
Then the form:
<form action="update_form.php">
<input type="text" name="sun_name" value="<?php echo $sun_info['name'] ?>" />
<input type="text" name="sun_price" value="<?php echo $sun_info['price'] ?>" />
Like I said the form updates fine but then when the form data is submitted to update_form.php I receive the error. Here is the form action update.php
$sun_day_change = $_POST['sun_name'];
$sun_price_change = $_POST['sun_price'];
$sunday = 'sunday';
$sql = "UPDATE mytable SET name = '$sun_day_change', price = '$sun_price_change' WHERE day = ".$sunday;
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
header('Location: http://localhost/form.php');
I'm assuming the issue is when I pass the values back to the database but since I have used almost the identical sql query to retrieve the values I'm not sure where I went wrong. Thanks for the help!

You are missing quotation marks in the update query, around $sunday.
$sql = "UPDATE mytable SET name = '$sun_day_change', price = '$sun_price_change' WHERE day = '".$sunday."'";

Related

How to insert row id to another table field using php mysql?

I have first table name is conference.
I want to insert table conference row id (81) to another second table name conf_dates
and that have field conf_id use for save conference row id
The issue is that value is empty insert in second table conf_dates as shown in second table image
Here is php code
<?php
$id = $_GET["id"];
$trans = array("strtdate"=>$strtdate,"enddate"=>$enddate,"conf_id"=>$id);
$query = $db->insert($trans,PREFIX."conf_dates");
?>
Here is html code
<p>
<label>Conference ID</label>
<input name="conf_id" class="text-input medium-input" type="text" id="conf_id" value="" />
</p>
Please tell me how to insert row id to another table field?
what are the values of $strtdate, $enddate
Also please share the table structure of "conf_dates" .
$id = intval($_GET["id"]);
check you are getting correct types, and values for variables you trying to insert
You named the input field conf_id while you try to insert $_GET['id'].
You should insert $_GET['conf_id']` instead.

PHP MYSQL, 1292: Incorrect date value: '' for column 'sale_date' at row 1

I have a MySQL database with a clients table and a person(used for testing) table. My HTML, PHP, and scripts work as expected in the person table. When using the same coding in my clients table I get the following error "1292: Incorrect date value: '' for column 'sale_date' at row 1.
My HTML code is as follows:
<label>Sale Date:&nbsp <input id="cSaleDate" class="cSale" type="date" name="cSaleDate"></label>
My PHP code:
$sale_date = mysqli_real_escape_string($db,$_POST['cSaleDate']);
if (isset($_POST['cSaleDate']))
{
include_once('client.php');
$saleDate = date('0000-00-00', strtotime($sale_date));
if (!empty($_POST['cSaleDate']))
{
$saleDate = date('Y-m-d', strtotime($sale_date));
}
}
$fields = 'fips_code, size_code, client_name, client_state, sale_date';
$values = "'$fips_code', '$size_code', '$client_name', '$client_state', '$saleDate'";
$sql = "INSERT INTO clients ($fields) VALUES ($values)";
The column for the sale date is set as:
Name: sale_date
Datatype: DATE
Default: NULL
As mentioned before, this code does work with dates in the "person" table, but not with the "clients" table. I have the date column setup the same on both tables. Any help or ideas on why this works for one table but not the other would be greatly appreciated.
Your code largely depends on $_POST['cSaleDate'] and from the snippet you've provided if you don't actually get that value then $saleDate would be undefined. That is my best guess at what the problem may be.
Here is a possible way to rewrite that snippet:
// It seems you want this as your default if there is no $_POST['cSaleDate']?
$saleDate = '0000-00-00';
// Now check if you should overwrite this default
if (isset($_POST['cSaleDate'])) {
include_once('client.php');
if ( ! empty($_POST['cSaleDate'])) {
$saleDate = date('Y-m-d', strtotime($_POST['cSaleDate']));
}
}
// Now do your escape and db insert
$saleDate = mysqli_real_escape_string($db, $saleDate);
$fields = 'fips_code, size_code, client_name, client_state, sale_date';
$values = "'$fips_code', '$size_code', '$client_name', '$client_state', '$saleDate'";
$sql = "INSERT INTO clients ($fields) VALUES ($values)";
Thanks all for the help. I ended up using a ternary statement in the insert, which allowed me to insert 'NULL' into the date field in the database if the input is left blank.
$sql = "INSERT INTO table (SaleDate) VALUES (" . ($saleDate == '' ? 'NULL' : "'$saleDate'") . ")"

prevent insert data to database if specific atribute already exist

I want to prevent duplicate values into a database table from a form using PHP.
I have a table data on database that have atribute dataid(auto increment as primary key), data1, data2.
And I have a simple form like this
<h2>Enter your data</h2>
<form action="script.php" method="post">
Data 1:<input type="text" name="data1" /></p>
Data 2:<textarea name="data2"></textarea></p>
<input type="submit" name="submit" value="Add Data" />
</form>
It's script.php for inserting data to database
<?php
if(isset($_POST['submit']))
{
//connect to the database
$conn = mysql_connect('host', 'username', 'password', 'dbname') or die(mysql_error());
//insert results from the form input
$query = "INSERT INTO data(data1, data2) VALUES('$_POST[data1]', '$_POST[data2]')";
$result = mysql_query($conn, $query) or die(mysql_error());
}
?>
but it will insert any data from the form to database.
How can I prevent insert data to database if data1 already exist?
The solution to your problem is to make the column unique so the database takes care of enforcing this constraint. You can do this by creating a unique index or constraint:
alter table data add constraint unq_data_data1 unique (data1);
You can then use this in an insert to ignore duplicates by using on duplicate key update:
INSERT INTO data(data1, data2)
VALUES('$_POST[data1]', '$_POST[data2]')
ON DUPLICATE KEY UPDATE data1 = VALUES(data1);
The ON DUPLICATE KEY part doesn't really do anything. It is a no-op.
Also, you should parameterize your queries, so you are not subject to SQL injection and so the query plans can be cached. But that is another matter.
make column data1 unique
ALTER TABLE `data` ADD UNIQUE (`data1`);
change your query like this to ignore insert when data exist
$query = "INSERT IGNORE INTO data(data1, data2) VALUES('$_POST[data1]', '$_POST[data2]')";

how to i make a form update page to update database table records, but with multiple records at the same time?

i have a table like this:
id product_category product_name product_range discount_amt
---------------------------------------------------------------------------
1 Post Card 4x6 5M to 9,999 0.007
2 Post Card 4x6 10M to 14,999 0.01
3 Post Card 4x6 15M to 19,999 0.013
4 Post Card 4x6 20M to 24,999 0.015
5 Post Card 4x6 Over 25M 0.019
i'm calling just the discount_amt column into page like this:
$pricediscountquery = mysql_query("SELECT * FROM pricing_discount") or die(mysql_error());
$i=0;
while($pricingdiscountrow = mysql_fetch_array( $pricediscountquery )) {
$pricingdiscountarray[$i++]=$pricingdiscountrow['discount_amt'];
}
i'm displaying form fields and discount amounts prefilled in the values of the form fields like this:
<p>5M to 9,999: <input type="text" name="pc_4x6_5m_to_9999" value="<?php echo $pricingdiscountarray[0]; ?>" /></p>
<p>10M to 14,999: <input type="text" name="pc_4x6_10m_to_14999" value="<?php echo $pricingdiscountarray[1]; ?>" /></p>
<p>15M to 19,999: <input type="text" name="pc_4x6_15m_to_19999" value="<?php echo $pricingdiscountarray[2]; ?>" /></p>
<p>20M to 24,999: <input type="text" name="pc_4x6_20m_to_24999" value="<?php echo $pricingdiscountarray[3]; ?>" /></p>
<p>Over 25M: <input type="text" name="pc_4x6_over_25m" value="<?php echo $pricingdiscountarray[4]; ?>" /></p>
i'm creating my post variables like this:
$pc_4x6_5m_to_9999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_5m_to_9999']));
$pc_4x6_10m_to_14999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_10m_to_14999']));
$pc_4x6_15m_to_19999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_15m_to_19999']));
$pc_4x6_20m_to_24999 = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_20m_to_24999']));
$pc_4x6_over_25m = mysql_real_escape_string(htmlspecialchars($_POST['pc_4x6_over_25m']));
i'm trying to update multiple rows/records at the same time, just in the discount_amt column like this:
mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,$pc_4x6_5m_to_9999),(2,$pc_4x6_10m_to_14999),(3,$pc_4x6_15m_to_19999),(4,$pc_4x6_20m_to_24999),(5,$pc_4x6_over_25m) ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());
the one above doesn't create error in code, but it creates error on web page: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '),(2,),(3,),(4,),(5,) ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt)' at line 1
also tried this:
mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,'$pc_4x6_5m_to_9999'),(2,'$pc_4x6_10m_to_14999'),(3,'$pc_4x6_15m_to_19999'),(4,'$pc_4x6_20m_to_24999'),(5,'$pc_4x6_over_25m') ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());
this gave an error in code so i didn't even save it and try to run it:
mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,"$pc_4x6_5m_to_9999"),(2,"$pc_4x6_10m_to_14999"),(3,"$pc_4x6_15m_to_19999"),(4,"$pc_4x6_20m_to_24999"),(5,"$pc_4x6_over_25m") ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());
this also gave error in code:
mysql_query("INSERT INTO pricing_discount (id,"discount_amt") VALUES (1,"$pc_4x6_5m_to_9999"),(2,"$pc_4x6_10m_to_14999"),(3,"$pc_4x6_15m_to_19999"),(4,"$pc_4x6_20m_to_24999"),(5,"$pc_4x6_over_25m") ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());
if you hard code values like this it works, but i'm doing variables:
mysql_query("INSERT INTO pricing_discount (id,discount_amt) VALUES (1,1),(2,3),(3,3),(4,12),(5,12) ON DUPLICATE KEY UPDATE discount_amt=VALUES(discount_amt); ") or die(mysql_error());
what's the correct way to do this?
if you're wondering why i didn't do UPDATE instead of INSERT when trying to update records, it's because i was following this stackoverflow:
Multiple Updates in MySQL
I am assuming, in your table discount_amt will be float or double datatype. then below sql query qill work. for int, float, decimal or double datatype no need to add value between single quote'.
$query = "
INSERT INTO pricing_discount
(id
,discount_amt
) VALUES
(1,".$pc_4x6_5m_to_9999."),
(2,".$pc_4x6_10m_to_14999."),
(3,".$pc_4x6_15m_to_19999."),
(4,".$pc_4x6_20m_to_24999."),
(5,".$pc_4x6_over_25m.")
ON DUPLICATE KEY UPDATE discount_amt = VALUES(discount_amt);
";
mysql_query($query) or die(mysql_error());
Just to recap, what's wrong with this...
$query = "
INSERT INTO pricing_discount
(id
,discount_amt
) VALUES
(1,'$pc_4x6_5m_to_9999'),
(2,'$pc_4x6_10m_to_14999'),
(3,'$pc_4x6_15m_to_19999'),
(4,'$pc_4x6_20m_to_24999'),
(5,'$pc_4x6_over_25m')
ON DUPLICATE KEY UPDATE discount_amt = VALUES(discount_amt);
";
mysql_query($query) or die(mysql_error());

Get the id of current mysql insert

is there anyway to get the ID of the current record I am INSERTING into the database table using php with Mysql without having to do an extra select to get the last ID?
FOr example, if my table has these columns, id, url, name
and if url consists of the domain name and current id as the query variable ex:
domainname.com/page.php?id=current_id
$sql="INSERT INTO Persons (id, url, name )
VALUES
('domainname.com/page.php?id=".**whats_the_current_id**."','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
as far as I know, there is no 'clean' way to find the ID you are about to insert (from what I understand from your question, this is what you want to know).
Two options in my opinion, starting with the ugly one: select max(id) from Persons, increment it with one, and hope that no insert's will mess this up for you. Like I said, its ugly, and -not- reliable.
A better option would be to first insert the record with a dummy value for url, and then retrieving the just inserted row's ID with mysql_insert_id(). Then update that record with the correct url value.
You asked for a way to retrieve the id without a select query following the insert query, but like I said, I don't think this is possible.
i use mysql_insert_id() for that. it works fine.
// pseudo-ish code
$query = "INSERT something .... "
$updated = $db->run_query($query);
$id = mysql_insert_id();
your table should be like this
ID AUTO_INCREMENT
person_id VARCHAR
person_url ...
person_name ...
your post form something like
<form method="post">
<input type="hidden" name="id" value="<?php echo uniqid() ?>" />
...
</form>
the query should be like this:
$person_id = intval($_POST['id']);
$person_url = mysql_real_escape_string($_POST['url']);
$person_name = mysql_real_escape_string($_POST['name']);
mysql_query("INSERT INTO Persons (person_id, persno_url, person_name) VALUES ( {$person_id} , {$person_url}, {$person_name} )");
$ID = mysql_insert_id();
The current ID is in $_GET['id']. You should sanitize it before inserting it into your query:
$id = intval($_GET['id']);
Then, use $id in your query.
If you add classes around the first insert and then the second select. The select will work then.
<?php
class insert1{
function inserthere(){
***insert***
}
}
class select1{
function selecthere(){
***select***
}
}
$a = new insert1;
$a->inserthere();
$b = new select1;
$b->selecthere();
?>

Categories