Php and MySQL question
Moderator: General Moderators
Php and MySQL question
Hi folks. I have this question. If lets say in my PHP i got to enter a few entries, eg. Name Age Address DOB and I got to put it into mysql, do I have to put the arrangement according to name age addy and dob in mysql. Why i'm asking becoz I was trying to mod a script and I changed the viarables and the descriptions to the things i need and also added a few in php and also in sql.
Is it alright to post the code? here?
Is it alright to post the code? here?
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Code: Select all
$query = "INSERT INTO homes (title, address, city, state, zip, price, rentmort, previewdesc, fulldesc, type, imageloc, beds, baths, status, featured, mls, neighborhood, agent, agenturl, agentemail, bcny, pto, hgfl, mdfl, lwfl, corn, dtod, corr, lift, ns, seav, blok, mw, gas, tv, frdg, wash, crfl, mrfl, gnfl, prfl, sch, mrkt, cfsp, mrt, golf, pool, spa, gym, tns, crt, park, play, clb, gtd, notes, owner, yearbuilt, sqfeet, lotsize, numfloors, garagesize, proptax, country, virtualtour, dateadded) values ('$title', '$address', '$city', '$state', '$zip', '$price', '$rentmort', '$previewdesc', '$fulldesc', '$type', '$imageloc', '$beds', '$baths', '$status', '$featured','$mls', '$neighborhood', '$agent', '$agenturl', '$agentemail', '$bcny', '$pto', '$hgfl', '$mdfl', '$lwfl', '$corn', '$dtod', '$corr', '$lift', '$ns', '$seav', '$blok', '$mw', '$gas', '$alrm', '$tv', '$frdg', '$wash', '$crfl', '$mrfl', '$,gnfl', '$prfl', '$sch', '$mrkt', '$cfsp', '$mrt', '$golf', '$pool', '$spa', '$gym', '$tns', '$crt', '$park', '$play', '$clb', '$gtd', '$notes', '$current_user', '$yearbuilt', '$sqfeet', '$lotsize', '$numfloors', '$garagesize', '$proptax', '$country', '$virtualtour', '$dateadded')";
if (!mysql_query ($query, $link) )
{
die (mysql_error());
}
print "Your listing has been added...";
}Which puzzles me even i physically count the number of data created in the table tallies with entries.Column count doesn't match value count at row 1
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
That's a massive amount of data to be adding (i.e. lots of columns). I might be tempted to start just with just one column and value pair, e.g.:
and then add another few pairs at a time. I counted 65 columns but 66 values (which is why you're getting the error) so you probably need to start building the INSERT statement a bit at a time so that you can spot where the extra value has creapt in.
Edit: or of course someone else could spot it for you
spotters badge to markl999 
Mac
Code: Select all
INSERT INTO homes (title) values ('$title')Edit: or of course someone else could spot it for you
Mac
markl999 wrote:Took me 3 goes to spot it though. If you stare at it long enough an image of Rasmus Lerdorf appears like some weird optical illusion
I'm confused. But anyway I redid the whole thing againn and it worked. I dun know why.... REALLY WIERD!! This time I changed one parameter at a time whihch took me near to forever.
Instead of defining each column and corresponding value manually:
Code: Select all
<?php
/*
kriek at phpfreaks dot com
*/
$host = 'localhost';
$user = 'username';
$pass = 'password';
$name = 'database';
$table = 'homes';
function quote(&$value, $key, $table) {
$value = mysql_escape_string($value);
}
function insert($rows, $table) {
array_walk($rows, 'quote', $table);
return sprintf('INSERT INTO %s(%s) VALUES(%s)',
$table,
join(', ', array_keys($rows)),
join(', ', array_values($rows)));
}
function columns($result) {
for ($i = 0; $i < mysql_num_fields($result); $i++) {
$columnsї] = mysql_field_name($result, $i);
}
return implode(',', $columns);
}
$conn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($name, $conn) or die(mysql_error());
$query = "SELECT * FROM $table";
$result = mysql_query($query, $conn) or die(mysql_error());
$columns = columns($result);
$values = implode(',', $_POST);
$rows = array($columns => $values);
mysql_query(insert($rows, $table), $conn) or die(mysql_error());
mysql_free_result($result) or die(mysql_error());
mysql_close($conn) or die(mysql_error());
?>