Jump to content

[SOLVED] Update Whole Table


ayok

Recommended Posts

Hi,

Is it possible to update the whole rows on a table with UPDATE table SET?

I would like to replace some data's in a table with some new data's. Thus, the old datas should be all removed and new datas are inserted. I've tried to delete all data's first and entry some new data's, but it doesn't work. I keep getting "Query was empty" error message.

 

Here is my try:

<?php
include "../../connection/db.php";
............
$delete=mysql_query("DELETE * FROM form");
$input=mysql_query($delete) or DIE(mysql_error());
............
$input=mysql_query("INSERT INTO form(picked,label,codes) VALUES('$picked','$label','$codes')") or DIE(mysql_error());
if($input)
{
echo "success";
}
else
{
echo "failed!";
}
?>

 

Anyone help?

 

Thank you,

ayok

Link to comment
Share on other sites

Where do you assign data to these variables?

 

$picked,$label,$codes

 

I made a form which send the value of those variables to be inserted to the table. I can insert those to the table without deleting the old data's first. But I need to delete the old first.

 

Thanks

ayok

Link to comment
Share on other sites

The DELETE * FROM form should delete all rows in the table form.

 

But you are asking two different things.  Your subject is UPDATE whole table.  That isn't what you are doing, per say.  You are deleting all rows and inserting new rows. 

 

The error that you query is empty is coming from the INSERT because you are not sending anything over.  You need to assign data to those variables from your FORM.  Where are your $_POST variables?

Link to comment
Share on other sites

Hi revraz,

What I'm trying to achieve is that to have new datas on my table. There is a form with check boxes (name='pick')for sending the datas.

Here is the whole update.php:

<?php
$id_temp = $_POST['id_temp'];
$pick = $_POST['pick'];
$label_temp = $_POST['label_temp'];
$codes_temp = $_POST['codes_temp'];

include "../../connection/db.php";
$delete=mysql_query("DELETE * FROM form");
$input=mysql_query($delete) or DIE(mysql_error());
foreach($pick as $id_temp){
$select=mysql_query("SELECT * FROM temp_form WHERE id_temp = '$id_temp'");
while ($data=mysql_fetch_array($select))
{
	$label=$data[label_temp];
	$codes=$data[codes_temp];
	$input=mysql_query("INSERT INTO form(picked,label,codes) VALUES('$id_temp','$label','$codes')") or DIE(mysql_error());
}
}

if($input)
{
	header("location:../../form/form.php");
}
else
{
	 echo "failled!";
}

?>

If i don't use this codes

<?php
$delete=mysql_query("DELETE * FROM form");
$input=mysql_query($delete) or DIE(mysql_error());
?>

I can insert new datas after I delete the old datas manually from phpmyadmin. But with those codes i keep getting error message: Query was empty.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.