Php Question ( Insert Sql Thingy)

  Post new topicReply to topicPrintable Version
<< View previous topic View next topic >>
Share: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
#1   Php Question ( Insert Sql Thingy)
rlgnak
CZ Super Newbie
rlgnak has been a member for over 20 year's 20 Year Member
usa.gif alaska.gif
Occupation: College
Gender: Male
Status: Offline
Joined: Jun 22, 2004
0.01 posts per day
Posts: 62
Points: 3,367
   
Im'a Trying To Insert Data Into A Databse Using Html Input Boxes And When Ever I Hit My Homemade Submit Button It Sends Blank Info Into The Databse Could Anyone Help Me With This Thanx icon_biggrin.gif



<?php
$sql = "SELECT id, name, msn, aol, yahoo , icq
       FROM  lod_im";

$result = mysql_query($sql);

if (!$result) {
   echo "Could not successfully run query ($sql) from DB: " . mysql_error();
   exit;
}

if (mysql_num_rows($result) == 0) {
   echo "No rows found, nothing to print so am exiting";
   exit;
}
<table BORDER="2"><tr><td >Id</td><td width="20%">Member Name</td><td width="20%">Msn Addy</td><td width="20%">Aol Addy</td><td width="20%">Yahoo Addy</td><td width=20%">E-Mail Addy</td></tr>';
while ($row = mysql_fetch_assoc($result)) {
   echo '<tr><td>';
   echo $row["id"];
   echo '</td><td>';
   echo $row["name"];
   echo '</td><td>';
   echo $row["msn"];
   echo '</td><td>';
   echo $row["aol"];
   echo '</td><td>';
   echo $row["yahoo"];
   echo '</td><td>';
   echo $row["icq"];
   echo '<br>';
}
echo '</tr></table>';
mysql_free_result($result);


if (isset($_POST["tekst1"])){

$sql = mysql_query("INSERT INTO lod_im (name,msn,yahoo) VALUES('".$_POST["text1"]."','".$_POST["text2"]."','".$_POST["text3"]."')")or die(mysql_error());


    if ($sql){
        print "Data Inserted";
    }
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Untitled</title>
</head>

<body>
<form name="f1">
Lod User Name<input type="text" name="text1" value="">
<br>
Msn Messenger <input type="text" name="text2" value="">
<br>
Yahoo Instant Messenger <input type="text" name="text3" value="">
<br>
<input type="submit" value="Submit">
</form>


</body>
</html>








Back to top Reply with quote
#2   re: Php Question ( Insert Sql Thingy)
Kelly_Hero
PayPal Donation
CZ Revered Member
 Codezwiz Site Donator
usa.gif southcarolina.gif
Occupation: Web Developer
Age: 59
Gender: Female
Website:
Status: Offline
Joined: Aug 20, 2003
0.49 posts per day
Posts: 3765
Points: 351,412
   
With the code that you have, no data is even being pulled from your form. Let's look at this bit by bit:
}
<table BORDER="2"><tr><td >Id</td><td width="20%">Member Name</td><td width="20%">Msn Addy</td><td width="20%">Aol Addy</td><td width="20%">Yahoo Addy</td><td width="20%">E-Mail Addy</td></tr>';
while ($row = mysql_fetch_assoc($result)) {


1. You either need to close your php before you start your table code and reopen it before your while statement, or you need to have php writing the beginning of your table code. I would do it the second way to keep your coding consistent:

}
echo "<table BORDER=\"2\"><tr><td >Id</td><td width=\"20%\">Member Name</td><td width=\"20%\">Msn Addy</td><td width=\"20%\">Aol Addy</td><td width=\"20%\">Yahoo Addy</td><td width=\"20%\">E-Mail Addy</td></tr>";
while ($row = mysql_fetch_assoc($result)) {


2. You have no action or method attibutes in your form tag. The method, either GET or POST determines how the information from your form gets passed back to the server for processing. The action specifies the URL that you wish the form's data to be sent to upon submission. Without these attributes, no data from your form is even being sent to the database.

3. In this bit of code:
if (isset($_POST["tekst1"])){

This code is basically saying "if "tekst1" exists, run the sql statement and insert the data into the database. You don't even have tekst1 in your form, so your sql statement shouldn't even be running.

4. You really should name your input fields according to the corresponding fields in your databalse. For example,
Lod User Name<input type="text" name="text1" value="">
<br>
Msn Messenger <input type="text" name="text2" value="">
<br>
Yahoo Instant Messenger <input type="text" name="text3" value="">
<br>
<input type="submit" value="Submit">


should be:
Lod User Name<input type="text" name="name" value="">
<br>
Msn Messenger <input type="text" name="msn" value="">
<br>
Yahoo Instant Messenger <input type="text" name="yahoo" value="">
<br>
<input type="submit" value="Submit">

because that's how you have the fields in the lod_im table named.

Your SQL statement would then look like this:
$sql = mysql_query("INSERT INTO lod_im (name,msn,yahoo) VALUES('".$_POST["name"]."','".$_POST["msn"]."','".$_POST["yahoo"]."')")or die(mysql_error());


5. And last, not sure if you are aware, or if you just did it for posting purposes, but your form has input fields for name, msn and yahoo, but your table has fields for name, msn, yahoo and icq. (We'll assume id is the number assigned to the row when the record is added to the database.) So basically, you're trying to pull information out of the database that your form isn't even putting in to start with.

Hope this helps.


Back to top Reply with quote
#3   re: Php Question ( Insert Sql Thingy)
rlgnak
CZ Super Newbie
rlgnak has been a member for over 20 year's 20 Year Member
usa.gif alaska.gif
Occupation: College
Gender: Male
Status: Offline
Joined: Jun 22, 2004
0.01 posts per day
Posts: 62
Points: 3,367
   
Ahh That Help Heaps Thankx One Last Question tho when i click sumbit it sends the data but is there a way to redirect after ive hit submit to stop double and tripe posting from accidentel refresh or something ..Oh and a way to stop when somone just refreshs the page it sends the data not very sure about that well heres the code edited


<html>
<head>
    <title>Untitled</title>
</head>

<body>
<form name="f1" method="post">
<table border="2">
<tr><td>Lod User Name</td><td><input type="text" name="name" value=""></tr></td>
<br>
<tr><td>Msn Messenger</td><td><input type="text" name="msn" value=""></tr></td>
<br>
<tr><td>Yahoo Instant Messenger</td><td><input type="text" name="yahoo" value=""></tr></td>

<tr><td>Aol Instant Messenger</td><td><input type="text" name="aol" value=""></tr></td>

<tr><td>Email</td><td><input type="text" name="text5" value=""></tr></td>

</table>
<input type="submit" value="Submit" >    <input type="button" name="Exit" value="  Exit   "
onClick="javascript:location.href='http://microsoft.com';">
</form>


</body>
</html>

<?php
$hostname = "xxxxxxxxxx";
$usernamedb = "xxxxxx";
$passworddb = "xxxxxxx";
$dbName = "xxxxxx";

//Edited KH -- Do not post username/passwords in your code!

MYSQL_CONNECT($hostname, $usernamedb, $passworddb);
@mysql_select_db("$dbName");



$sql = "SELECT id, name, msn, aol, yahoo , icq
       FROM  lod_im";

$result = mysql_query($sql);

if (!$result) {
   echo "Could not successfully run query ($sql) from DB: " . mysql_error();
   exit;
}

if (mysql_num_rows($result) == 0) {
   echo "No rows found, nothing to print so am exiting";
   exit;
}
echo "<table BORDER=\"2\"><tr><td >Id</td><td width=\"20%\">Member Name</td><td width=\"20%\">Msn Addy</td><td width=\"20%\">Aol Addy</td><td width=\"20%\">Yahoo Addy</td><td width=\"20%\">E-Mail Addy</td></tr>";
while ($row = mysql_fetch_assoc($result)) {
   echo '<tr><td>';
   echo $row["name"];
   echo '</td><td>';
   echo $row["msn"];
   echo '</td><td>';
   echo $row["aol"];
   echo '</td><td>';
   echo $row["yahoo"];
   echo '</td><td>';
   echo $row["icq"];
   echo '<br>';
}
echo '</tr></table>';
mysql_free_result($result);



$sql = mysql_query("INSERT INTO lod_im (name,msn,yahoo,aol,icq) VALUES('".$_POST["name"]."','".$_POST["msn"]."','".$_POST["yahoo"]."','".$_POST["aol"]."','".$_POST["icq"]."')")or die(mysql_error());
    if ($sql){
     

}
?>












Back to top Reply with quote
#4   re: Php Question ( Insert Sql Thingy)
Kelly_Hero
PayPal Donation
CZ Revered Member
 Codezwiz Site Donator
usa.gif southcarolina.gif
Occupation: Web Developer
Age: 59
Gender: Female
Website:
Status: Offline
Joined: Aug 20, 2003
0.49 posts per day
Posts: 3765
Points: 351,412
   
If you look at your code above, you will see I have edited out your database information. You should never ever post that information as you have just compromised the security of your database. You need to change your username and password ASAP.

The following is an example of how to redirect after your form is submitted:
  $insertGoTo = "whatever.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}



Back to top Reply with quote
Display posts from previous:      
Add To: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
<< View previous topic View next topic >>
Post new topicReply to topic

Jump to 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum