Jump to content

[SOLVED] Bit of a problem


amp_rsa

Recommended Posts

hi need help witha problem ive come across...i've got a form that i use on my web page which uses a mysql database....the prob is every time i reloead the page that the form is on it adds a blank record into my mysql database! Help please..what am i doing wrong! here is my code:

<title>RB AH</title>
</head>

<body>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("rbah", $con);

$sql="INSERT INTO item (username, name2, item, details, price)
VALUES
('$_POST[username]','$_POST[name2]','$_POST[item]','$_POST[details]','$_POST[price]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

$result = mysql_query("SELECT * FROM item");

echo "<table border='1'>
<tr>
<th>username</th>
<th>name2</th>
<th>item</th>
<th>details</th>
<th>price</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['name2'] . "</td>";
  echo "<td>" . $row['item'] . "</td>";
  echo "<td>" . $row['details'] . "</td>";
  echo "<td>" . $row['price'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

?>

<form action="test.php" method="post">
Username:<br/><input name="username" type="text" size="23" /><br/>
Character name:<br/><input name="name2" type="text" size="23" /><br/>
Item:<br/><input name="item" type="text" size="39" /><br/>
Item Details:<br/><input name="details" type="text" size="39" />
<br/>
Price:<br/><input name="price" type="text" size="10" /><br/><br/>
<input name="submit" type="submit" />
</form>


</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/67254-solved-bit-of-a-problem/
Share on other sites

This is what you need i think but if it nothing hasnt been entered into the script the user will jsut get a blank page, so you might wish to add so kind of redirect or page telling them to fill in the from .

<title>RB AH</title>
</head>

<body>
<?php
  if (isset($_POST['submit'])) {
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("rbah", $con);

$sql="INSERT INTO item (username, name2, item, details, price)
VALUES
('$_POST[username]','$_POST[name2]','$_POST[item]','$_POST[details]','$_POST[price]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

$result = mysql_query("SELECT * FROM item");

echo "<table border='1'>
<tr>
<th>username</th>
<th>name2</th>
<th>item</th>
<th>details</th>
<th>price</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['name2'] . "</td>";
  echo "<td>" . $row['item'] . "</td>";
  echo "<td>" . $row['details'] . "</td>";
  echo "<td>" . $row['price'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
}
?>

<form action="test.php" method="post">
Username:<br/><input name="username" type="text" size="23" /><br/>
Character name:<br/><input name="name2" type="text" size="23" /><br/>
Item:<br/><input name="item" type="text" size="39" /><br/>
Item Details:<br/><input name="details" type="text" size="39" />
<br/>
Price:<br/><input name="price" type="text" size="10" /><br/><br/>
<input name="submit" type="submit" />
</form>


</body>
</html>

thanx it did help... but i ended up splitting it in to 3 files with that bit of code in in the php file that inserts it into mysql! I need help on something else though! this is my code:

<?php
  if (isset($_POST['submit'])) {
$con = mysql_connect("localhost","root","amped_99!");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("amp26_rbah", $con);

$sql="INSERT INTO item (username, name2, item, details, price)
VALUES
('$_POST[username]','$_POST[name2]','$_POST[item]','$_POST[details]','$_POST[price]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
  mysql_close($con);
}
?>

What i need is, after the details have been inserted in to mysql, iwant users to be directed back to anouther page..because at the moment it stays on this one, which is blank...if you know what i mean! help pls

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.