Page 1 of 1

Php and MySQL question

Posted: Tue Oct 21, 2003 8:16 am
by JimBeam
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?

Posted: Tue Oct 21, 2003 8:35 am
by twigletmac
Please post the code, it'll help us see better what you are trying to do :)

Mac

Posted: Tue Oct 21, 2003 9:12 am
by JimBeam
okie i try to give a small part,

Posted: Tue Oct 21, 2003 9:24 am
by JimBeam

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...";
		}
Ok all the above are in the SQL database. however I still get
Column count doesn't match value count at row 1
Which puzzles me even i physically count the number of data created in the table tallies with entries.

Posted: Tue Oct 21, 2003 9:30 am
by markl999
mw, gas, tv, frdg ....
...
'$mw', '$gas', '$alrm', '$tv', '$frdg'
You appear to be missing 'alrm' in the column list.

Posted: Tue Oct 21, 2003 9:36 am
by twigletmac
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.:

Code: Select all

INSERT INTO homes (title) values ('$title')
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 :lol: spotters badge to markl999 :)

Mac

Posted: Tue Oct 21, 2003 9:40 am
by markl999
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 :o

Posted: Wed Oct 22, 2003 1:17 am
by JimBeam
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 :o
:?:

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. :) I'll try to compare and pin it up here. :) Hope fully others won't make the same mistakes I do. :)

Posted: Wed Oct 22, 2003 2:02 am
by Kriek
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) &#123; 
        $value = mysql_escape_string($value); 
    &#125; 
    function insert($rows, $table) &#123; 
        array_walk($rows, 'quote', $table); 
        return sprintf('INSERT INTO %s(%s) VALUES(%s)', 
            $table, 
            join(', ', array_keys($rows)), 
            join(', ', array_values($rows))); 
    &#125; 
    function columns($result) &#123; 
        for ($i = 0; $i < mysql_num_fields($result); $i++) &#123; 
            $columns&#1111;] = mysql_field_name($result, $i); 
        &#125; 
        return implode(',', $columns); 
    &#125; 
    $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()); 
?>