Problem writing to database

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

Will_Irish
Forum Newbie
Posts: 6
Joined: Thu Sep 23, 2004 5:18 pm

Problem writing to database

Post by Will_Irish »

Hi

I have php dev 4.2.3 installed on my pc at home to test scripts out and everything works fine. I have now decided to try writing some scripts my self but I am having trouble writing to databases. I can manage to create the databases but for some reason I cant seem to write to them. I have visited websites looking for tutorials on this subject and have tried the scripts but these wont write to the database either. I cant understand this because I have no problem writing to the databases with the professional applications eg phpBB etc.

If you would like to see the script I am having trouble with it can be found here in a zip file: http://www.tipperary-central.com/contacts.zip .

Thanks
Will
User avatar
Gonik
Forum Newbie
Posts: 19
Joined: Fri Aug 30, 2002 7:39 am
Location: Somewhere Around Nothing

Post by Gonik »

When developing code it's a good idea to include some very basic debugging methods. For instance:

Code: Select all

<?php
if(!function_name(function_args)) {
  print("Function <i>function_name</i> failed to execute");
  die();
}
?>
This is generally good idea to do also in MySQL-like functions:

Code: Select all

<?php
mysql_connect($host,$usr,$pass) or die(mysql_error());
?>
or for the query execution:

Code: Select all

<?php
mysql_query($query_var) or die(mysql_error());
?>
the mysql_error() function will display a user-friendly message if an error occurs in MySQL.

Anyhow, your problem seems rather easy. Open all files in your favorite editor and replace localhost with "localhost" (you need the quotes), in your mysql_connect() arguments.
After that, it seems that your form doesn't parse when submitted. This is due to the million-said and solved problem of register_globals in php.ini...

Check this link on PHP.net to know how you can fix your scripts.
Will_Irish
Forum Newbie
Posts: 6
Joined: Thu Sep 23, 2004 5:18 pm

Post by Will_Irish »

Thanks Gonik

I'll try that out and see what happens. I am new to this stuff as you probably guessed. :wink:
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

FYI

post the code using the [syntax=php]and[/syntax][syntax=php]tags in the future

people are here to help, but downloading a file isnt something everyone wants to do (with the threats of virus and such bs that is out there)

Not accusing or pointing the finger, just saying for future post you have may use the phpbb tags, it makes us help you so much easier[/syntax]
Will_Irish
Forum Newbie
Posts: 6
Joined: Thu Sep 23, 2004 5:18 pm

Post by Will_Irish »

Sorry about the code tags. My problem still persists. All it is doing is writing a blank line to the database. I manually added info to the database and my code can read no problem. Aghaaa!!

Here is the code:

dbinfo.inc.php

Code: Select all

<?
$username="";
$password="";
$database="contacts";
?>

setup.php

Code: Select all

<?
include("dbinfo.inc.php");
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE contacts (id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
mysql_query($query);
mysql_close(); 
echo "Database created";
?>

Index.php

Code: Select all

<?
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>";


?>

insert.php

Code: Select all

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

update.php

Code: Select all

<?
include("dbinfo.inc.php");
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result); 
mysql_close();

$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");

?>

<form action="updated.php">
<input type="hidden" name="ud_id" value="<? echo "$id"; ?>">
First Name: <input type="text" name="ud_first" value="<? echo "$first"?>"><br>
Last Name: <input type="text" name="ud_last" value="<? echo "$last"?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<? echo "$phone"?>"><br>
Mobile Number: <input type="text" name="ud_mobile" value="<? echo "$mobile"?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<? echo "$fax"?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br>
Web Address: <input type="text" name="ud_web" value="<? echo "$web"?>"><br>
<input type="Submit" value="Update">
</form>

<?
++$i;
} 
?>

updated.php

Code: Select all

<?
include("dbinfo.inc.php");
mysql_connect("localhost",$username,$password);

$query="UPDATE contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone', mobile='$ud_mobile', fax='$ud_fax', email='$ud_email', web='$ud_web' WHERE id='$ud_id'";
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
echo "Record Updated";
mysql_close();
?>

I have a feeling in my gut its something with up with my server settings. Could I be right??
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

well I see you used the or die (myql_error); once or twice, but you didnt use it in your insert.php

its wise to troubleshoot ANY mysql-related issues with this tactic

use this line instead of your own:

Code: Select all

<?php
$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web') or die (mysql_error())";
?>
also, echo out the $query var to see if the variables (ie, $first, $last, etc) are being past and read correctly.

no problem about the tags, just informing you thats what they are there for
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

and come to think of it, you may need to add in the $_POST superglobal to your variables you are trying to add tot he database as globals are off be default in the version of php you run.

$first = $_POST['first'];

or whatever u set-up your form, any more problems please post the code of your form.
User avatar
Gonik
Forum Newbie
Posts: 19
Joined: Fri Aug 30, 2002 7:39 am
Location: Somewhere Around Nothing

Post by Gonik »

um, tim i think you just made a typo :)
The or die(mysql_error()); must be after the mysql_query() function and not the query string.

Will, just do what tim said with the global varibles. Use $_POST['fildname'] as the var name, replacing fieldname with the value of the name attribute of each input field.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

eh, been along day

kudos for the catch
Will_Irish
Forum Newbie
Posts: 6
Joined: Thu Sep 23, 2004 5:18 pm

Post by Will_Irish »

I did what you said except for typing in $first = $_POST['first']; as I didnt know where to type it in. You are correct in saying that I have globals off as I looked in the php.ini file. I also inserted the echoed out the query var and all I got was(,,,,,,) so I guess nothing is even making it to the database.

My form is just in html:

Code: Select all

<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>
By the way thanks for helping me out.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

$_POST

Post by phpScott »

you will need to use $_POST[''] or $_GET[''] depending on your form method when ever you are passing variables between pages.

in your insert.php try

Code: Select all

<?php
if(isset($_POST['first']))
  $first=$_POST['first'];
if(isset($_POST['last']))
  $last=$_POST['last'];
//etc...  to get your variables from the previous page
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();

?>
on another note it is a good idea to use the long php tags as in <?php instead of <? as alot of php installations don't recognize <?

phpscott
Will_Irish
Forum Newbie
Posts: 6
Joined: Thu Sep 23, 2004 5:18 pm

Post by Will_Irish »

Hi Lads

phpScott that seems to have done the trick. I would love to know how it did the trick. Thanks a million to all of you for helping me out.

Can you guys reccemmend a good book on beginning php. A sort of idiots guide :lol: because I could do with it.

Also keep an eye out for more questions as I am sure I will be back.

Thanks again
Will
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Personally, I've found "The PHP Anthology" by Harry Fuecks a very good introduction if you know bits about PHP. For other recommendations, search the forum - there are plenty of book recommendation-threads.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

register globals is off by default for security reasons.

that means you using $first is no good unless they are on. you must use the super globals (ie, POST, GET, COOKIE, etc) to call upon variables when register globals are off.

assign every variable to a $_POST key and you should be set. hence why your form didnt work, hence why the variables weren't recoginzed.
Will_Irish
Forum Newbie
Posts: 6
Joined: Thu Sep 23, 2004 5:18 pm

Post by Will_Irish »

thanks Tim for explaining that and everyone else that helped. By the way Tim nice Camaro!
Post Reply