Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1918 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

php and mySQL

Thread title: php and mySQL
Closed Thread    
    Thread tools Search this thread Display Modes  
04-02-2007, 04:33 PM
#1
ali.black is offline ali.black
Status: I'm new around here
Join date: Feb 2007
Location:
Expertise:
Software:
 
Posts: 13
iTrader: 0 / 0%
 

ali.black is on a distinguished road

  Old  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
non-issue is offline non-issue
Status: I'm new around here
Join date: Aug 2006
Location:
Expertise:
Software:
 
Posts: 3
iTrader: 0 / 0%
 

non-issue is on a distinguished road

  Old

the actual function is called mysql_num_rows() not mysql_numrows() also, mysql_fetch_row() is better than mysql_result because it is returning the whole row.

check this out for more info http://us3.php.net/manual/en/function.mysql-result.php

04-04-2007, 11:27 AM
#3
Immersion is offline Immersion
Status: Senior Member
Join date: Dec 2005
Location:
Expertise:
Software:
 
Posts: 918
iTrader: 5 / 100%
 

Immersion is on a distinguished road

  Old

$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
ali.black is offline ali.black
Status: I'm new around here
Join date: Feb 2007
Location:
Expertise:
Software:
 
Posts: 13
iTrader: 0 / 0%
 

ali.black is on a distinguished road

  Old

cheers, ye i got it working

Thanks

04-05-2007, 01:26 AM
#5
powerspike is offline powerspike
Status: I'm new around here
Join date: Feb 2007
Location: Sydney, Australia
Expertise:
Software:
 
Posts: 23
iTrader: 0 / 0%
 

powerspike is on a distinguished road

  Old


$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.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed