mysql problems

They have: 71 posts

Joined: Mar 2004

Okay, I've got another one for you. I can not figure out why the mysql database won't insert these values! (I've taken out almost all the fields to make it easier).

if(isset($submit)){
//update the database with the information

$id = getUserID($cookie_name);

$sql = "INSERT into dailytracker (contactsmade,listingappointmentsmade) VALUES('$contactsmade','$listingappointmentsmade') where id=$id";

}
'
So if the form is submitted, insert into table dailytracker collumns contactsmade and listingappointmentsmade where the username matches who is currently logged in.

<input type='text'  style='text-align: right'  size='3' maxlength='5' name='contactsmade' value=\"".$user_info['contactsmade']."\" >
<input type='text'  style='text-align: right'  size='3' maxlength='5' name='listingappointmentsmade' value=\"".$user_info['listingappointmentsmade']."\" >
'
Here are the fields.

$user_info = getUserInfo(getUserID($cookie_name));'
Here is where I get the users info into the field values.

Then I submit. I don't know what I'm doing wrong here!!!

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

$sql = "INSERT INTO `table_name` (`column1`,`column2`) VALUES ('$data1','$data2');

for a new entry, and you can't 'insert into' a row that exists in that case it's

$sql = "UPDATE `table_name` SET `column_name` = 'value' WHERE `id` = '$id';

just remember, fields, and tables need `` (below the tilde~) around them, and strings need ' ' around them.

I suggest getting MySQLadmin, and just look at some of the queries it runs when updating information.

Busy's picture

He has: 6,151 posts

Joined: May 2001

You using the right one, INSERT vs UPDATE ?

do you get a value if you echo $sql ?

and do you have a $something = mysql_query($sql); ?

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

echoing $sql will just get you the query obviously.

and you don't need to do a $something = for the mysql_query($sql); but you can then do:

if($something)
{
echo "YAY IT WORKED!";
}
else
{
echo "bunk, still didn't work";
}

Busy's picture

He has: 6,151 posts

Joined: May 2001

CptAwesome wrote: echoing $sql will just get you the query obviously.

Isn't a bad idea to go right back to basics, finding out if the variables actually have values can be the answer. Easier than echoing all variables, especially if you have more than a couple.

Again the mysql_query() is helpful for fault finding. there are other ways of doing it, sure, but the end of the day you need to find the problem whether it be a missing quote, comma, brace, value or whatever.

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

I have to agree with what Busy said. The WHERE clause does not work with INSERT. It sounds like you're wanting to use UPDATE, not INSERT.

They have: 71 posts

Joined: Mar 2004

Thanks for the help, I was able to get it in the database.

Here is my new problem. I am trying to load the form with all the values for that certain date. (I have a date field in the database table). How can I tell the page that I want it to load the values only for a selected date?

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

SELECT field1, field2, field3 FROM tablename WHERE datefield = 'XX-XX-XXXX'

Then, of course, you'll want to use a while loop to go through the results for that date, unless your DB is set up so that there's only one entry per date.

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

if the date field is mixed date/time that won't work, but you could query and pull all results (depending on the size, this could be a bad idea) and then use an ereg function, or some other comparison method to analyze the info.

They have: 71 posts

Joined: Mar 2004

Thanks, sorry that was a stupid question. Smiling

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Good point, CptAwesome. You might have to do this:

SELECT field1, field2, field3 FROM tablename WHERE datefield LIKE 'XX-XX-XXXX %'

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.