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.
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]')";
I have this form and this function for insert name in db:
<form action="player.php#insertPlayer" method="post">
<input type="text" name="nameplayer" />
<input type="submit" value="Ajouter joueur" />
</form>
function insertPlayer($name) {
db connection
if (empty ($name))
{echo "<span style='color: red'>WRONG!!!!</span>";}
else
{
$insertplayer="INSERT INTO `player`(id, name) VALUES ('','$name');";
echo 'player insert succes';
mysql_close($db);
}
}
But if i enter 2 same name, it's work, how can i do for just have always one same name ?
To make player table column name as UNIQUE, run the below ALTER statement on your corresponding Database.
ALTER TABLE player ADD UNIQUE (name);
If already the player table has some data with duplicate values in name column, the alter statement will fail. So clear the data from player table before running the ALTER statement.
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());
I have a form where the user inserts data but they can go back to the same page to edit their information. My table structure is:
id (auto int index),
user id (links to other tables),
Doc_Name,
Abstract
I have an insert query:
$user_id = intval($_SESSION['user_id']);
$Doc_Name = mysql_real_escape_string($_POST['Doc_Name']);
$abstract = mysql_real_escape_string($_POST['abstract']);
$the_query = sprintf("INSERT INTO `document` (`user_id`,`Doc_Name`,`abstract`) VALUES
('%d','%s','%s')", $user_id, $Doc_Name, $abstract);
However, if their is already a row for this user_id then I want the update query instead:
mysql_query("UPDATE document SET `Doc_Name` = '$Doc_Name', 'abstract='$abstract'
WHERE id='$_SESSION[user_id]'") or die(mysql_error());
Also, so the user knows what they entered, I tried to use this echo in the text box but that didn't work either,
<textarea name="Doc_Name" style="width:500px; height:150px" type="text" id="Doc_Name"
value="<? echo $row_settings['Doc_Name']; ?>" size="300"> </textarea>
You can use INSERT ... ON DUPLICATE KEY UPDATE Syntax
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE
c=c+1;
You want the INSERT ... ON DUPLICATE UPDATE syntax
for the textarea you can use
<textarea name="Doc_Name" style="width:500px; height:150px" type="text" id="Doc_Name" size="300"><? echo $row_settings['Doc_Name']; ?></textarea>
everything between the tags is displayed and editable
EDIT: to the other posters: nice, did not know INSERT ON DUPLICATE
the query:
SELECT * FROM document WHERE id='{$SESSION['user_id']}'
php:
if(mysql_num_rows(mysql_query($query)) > 0) {
//code to be executed if id exists
}