Problem writing to database
Moderator: General Moderators
-
Will_Irish
- Forum Newbie
- Posts: 6
- Joined: Thu Sep 23, 2004 5:18 pm
Problem writing to database
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
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
When developing code it's a good idea to include some very basic debugging methods. For instance:
This is generally good idea to do also in MySQL-like functions:
or for the query execution:
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.
Code: Select all
<?php
if(!function_name(function_args)) {
print("Function <i>function_name</i> failed to execute");
die();
}
?>Code: Select all
<?php
mysql_connect($host,$usr,$pass) or die(mysql_error());
?>Code: Select all
<?php
mysql_query($query_var) or die(mysql_error());
?>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
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]
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
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
setup.php
Index.php
insert.php
update.php
updated.php
I have a feeling in my gut its something with up with my server settings. Could I be right??
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??
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:
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
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())";
?>no problem about the tags, just informing you thats what they are there for
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.
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.
-
Will_Irish
- Forum Newbie
- Posts: 6
- Joined: Thu Sep 23, 2004 5:18 pm
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:
By the way thanks for helping me out.
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>$_POST
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
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
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();
?>phpscott
-
Will_Irish
- Forum Newbie
- Posts: 6
- Joined: Thu Sep 23, 2004 5:18 pm
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
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
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
Also keep an eye out for more questions as I am sure I will be back.
Thanks again
Will
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.
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.
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