|
|
|
|
Thread title: php and mySQL |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
04-02-2007, 04:33 PM
|
#1
|
Status: I'm new around here
Join date: Feb 2007
Location:
Expertise:
Software:
Posts: 13
|
php and mySQL
hello, i found this tutorial on the net and i thought id give it a go because i have just started learning this stuff. the problem is when i input the info it seems to be ok but when i go to index.php new rows have been created but the info i inputted isnt displayed. could you please have a look at the code to see if there is a problem because i dont know what to look for. cheers.
dbinfo.inc.php(to connect to the database):
PHP Code:
<?
$username="name";
$password="pw";
$database="db";
?>
add.html(to input the data)
HTML Code:
<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>
insert.php(to insert the info into the database):
PHP Code:
<?
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();
?>
index.php (to display it):
PHP Code:
<?
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>";
?>
|
|
04-04-2007, 04:07 AM
|
#2
|
Status: I'm new around here
Join date: Aug 2006
Location:
Expertise:
Software:
Posts: 3
|
|
|
04-04-2007, 11:27 AM
|
#3
|
Status: Senior Member
Join date: Dec 2005
Location:
Expertise:
Software:
Posts: 918
|
$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$e mail','$web')";
you have not used the $_POST function to get the values. You will need to define them using
$first = $_POST['first'];
above the query (for each of them). You should also run some validation checks after it is working to ensure they are not empty etc.
if(empty($first)) {
echo "you did not fill in the first field";
exit;
}
Thats the kind of principle you will need to use.
|
|
04-04-2007, 09:01 PM
|
#4
|
Status: I'm new around here
Join date: Feb 2007
Location:
Expertise:
Software:
Posts: 13
|
cheers, ye i got it working
Thanks
|
|
04-05-2007, 01:26 AM
|
#5
|
Status: I'm new around here
Join date: Feb 2007
Location: Sydney, Australia
Expertise:
Software:
Posts: 23
|
$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$e mail','$web')";
|
i would recomend not using that as an insert method. The reason been if you change the database structor you'll have to go back and change every query that uses that style of data.
you should do something like
$query = "insert into contact (contact_id,first_name,last_name) VALUES('',mysql_escape_string($_POST['first']),mysql_escape_string($_POST['last']));
the mysql_escape_string should stop stuff like sql injections and stuff.
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|