ali_b Posted April 2, 2007 Share Posted April 2, 2007 Me agggain lol, trying another tutorial that i cant get to work Its a tutorial where u insert data into a database and display it but with some extra features such as linking to a website. the problem with it is that when i insert data i think it is inserted but when i go to the index page a new row is created but none of the info is displayed Maybe you could look through the code below and see if anything is wrong with it. i feel this is the best way for me to learn. Here how i connect to the database: <? $username="name"; $password="pw"; $database="db"; ?> Here is how i add info to the database: <form action="insert.php" method="post"> First Name: <input type="text" name="first"><br> Last Name: <input type="text" name="last"><br> Phone: <input type="text" name="phone"><br> Mobile: <input type="text" name="mobile"><br> Fax: <input type="text" name="fax"><br> E-mail: <input type="text" name="email"><br> Web: <input type="text" name="web"><br> <input type="Submit"> </form> Here is the php that adds it to the db: <? include("dbinfo.inc.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')"; mysql_query($query); mysql_close(); ?> and here is where it should be displayed: <? include("dbinfo.inc.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM contacts"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); echo "<b><center>Database Output</center></b><br><br>"; ?> <table border="0" cellspacing="2" cellpadding="2"> <tr> <th><font face="Arial, Helvetica, sans-serif">Name</font></th> <th><font face="Arial, Helvetica, sans-serif">Phone</font></th> <th><font face="Arial, Helvetica, sans-serif">Mobile</font></th> <th><font face="Arial, Helvetica, sans-serif">Fax</font></th> <th><font face="Arial, Helvetica, sans-serif">E-mail</font></th> <th><font face="Arial, Helvetica, sans-serif">Website</font></th> </tr> <? $i=0; while ($i < $num) { $first=mysql_result($result,$i,"first"); $last=mysql_result($result,$i,"last"); $phone=mysql_result($result,$i,"phone"); $mobile=mysql_result($result,$i,"mobile"); $fax=mysql_result($result,$i,"fax"); $email=mysql_result($result,$i,"email"); $web=mysql_result($result,$i,"web"); ?> <tr> <td><font face="Arial, Helvetica, sans-serif"><? echo "$first $last"; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><? echo "$phone"; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><? echo "$mobile"; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><? echo "$fax"; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo "$email"; ?>">E-mail</a></font></td> <td><font face="Arial, Helvetica, sans-serif"><a href="<? echo "$web"; ?>">Website</a></font></td> </tr> <? ++$i; } echo "</table>"; ?> Thank you very much for your help, its much appriciated ;D Ali Quote Link to comment Share on other sites More sharing options...
Caesar Posted April 2, 2007 Share Posted April 2, 2007 You're not specifying what fields you're inserting into! It should look something like this: <?php $db->connect(); $query = $db->query("INSERT INTO table (field_1, field_2) VALUES ('$value_1', '$value_2')"); $db->close(); ?> Quote Link to comment Share on other sites More sharing options...
ali_b Posted April 2, 2007 Author Share Posted April 2, 2007 What do these bits do? "->" could i do $query = "INSERT INTO contacts VALUES (first, blah, blah) ('$first','$last','$phone','$mobile','$fax','$email','$web')"; would that work? Quote Link to comment Share on other sites More sharing options...
ali_b Posted April 2, 2007 Author Share Posted April 2, 2007 ive tried this, <? include("dbinfo.inc.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO contacts VALUES(first,last,phone,mobile,fax,email,web) ('" . $first . "''" . $last . "','" . $phone . "','" . $mobile . "','" . $fax . "', '" . $email . "','" . $web . "',)"; mysql_query($query); mysql_close(); ?> but it still doesnt seem to work. any ideas? Quote Link to comment Share on other sites More sharing options...
per1os Posted April 2, 2007 Share Posted April 2, 2007 mysql_query($query) or die(mysql_error()); What is the error printing out? I bet the problem is where your "VALUES" lies, try this: $query = "INSERT INTO contacts (first,last,phone,mobile,fax,email,web) VALUES ('" . $first . "''" . $last . "','" . $phone . "','" . $mobile . "','" . $fax . "', '" . $email . "','" . $web . "',)"; Values are always second, basic principle of all SQL. Quote Link to comment Share on other sites More sharing options...
ali_b Posted April 2, 2007 Author Share Posted April 2, 2007 cheers, ill give that a go, i think there is soming wrong with the database aswell. On php myadmin thing it has a warning that says "PRIMARY and INDEX keys should not both be set for column `id`" How do i fix that? Quote Link to comment Share on other sites More sharing options...
ali_b Posted April 2, 2007 Author Share Posted April 2, 2007 if i insert to data using phpMyAdmin it is displayed so it is working lol just the insert page isnt Quote Link to comment Share on other sites More sharing options...
ali_b Posted April 3, 2007 Author Share Posted April 3, 2007 any1? Quote Link to comment Share on other sites More sharing options...
ali_b Posted April 3, 2007 Author Share Posted April 3, 2007 i worked out how to do it, problem solved Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.