Php and MySQL question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JimBeam
Forum Newbie
Posts: 4
Joined: Tue Oct 21, 2003 8:16 am
Contact:

Php and MySQL question

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Please post the code, it'll help us see better what you are trying to do :)

Mac
JimBeam
Forum Newbie
Posts: 4
Joined: Tue Oct 21, 2003 8:16 am
Contact:

Post by JimBeam »

okie i try to give a small part,
JimBeam
Forum Newbie
Posts: 4
Joined: Tue Oct 21, 2003 8:16 am
Contact:

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

mw, gas, tv, frdg ....
...
'$mw', '$gas', '$alrm', '$tv', '$frdg'
You appear to be missing 'alrm' in the column list.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
JimBeam
Forum Newbie
Posts: 4
Joined: Tue Oct 21, 2003 8:16 am
Contact:

Post 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. :)
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post 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()); 
?>
Post Reply