Jump to content

Creating a test script and htm page - not writing to table


Recommended Posts

PHP code:

 

<?
//login variable
$username = $_POST ['username'];
$password = $_POST ['password'];
$database = $_POST ['database'];
//table variable
        $table = $_POST ['table'];
//stats variables
        $name = $_POST ['name'];
        $force = $_POST ['force'];
        $armor = $_POST ['armor'];
        $strength = $_POST ['strength'];
        $value = $_POST ['value'];

mysql_connect ("localhost","$username","$password");
@mysql_select_db ($database) or die ("no database name");

$query = "INSERT INTO `$database`.`$table` ('name', 'force', 'armor', 'strength', 'value')
VALUES ('$name', '$force', '$armor', '$strength', '$value')";

mysql_query ($query);

if (mysql_query($query))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added<br>";

echo "<br>variable $name test";
echo "<br>variable $force test";
echo "<br>variable $armor test";
echo "<br>variable $strength test";
echo "<br>variable $value test";


mysql_close();

?>

 

 

HTML files code:

 

<form action = "datainput.php" method = "post">



<div align="center">Item Submission Form</div><br><br>


<br>
<div align="center"><input type="text" name="table" value="head neck back or chest" size="32" maxlength="32"></div>
<br>
<br>
<div align="center"><input type="text" name="name" value="Type Name Here" size="32" maxlength="32"></div>
<br>
<br>
<div align="center"><input type="text" name="force" value="Type force number" size="32" maxlength="32"></div>
<br>
<br>
<div align="center"><input type="text" name="armor" value="Type armor value" size="32" maxlength="32"></div>
<br>
<br>
<div align="center"><input type="text" name="strength" value="Type strength value" size="32" maxlength="32"></div>
<br>
<br>
<div align="center"><input type="text" name="value" value="Type cost Here" size="32" maxlength="32"></div>
<br>
<br>
<br>
<div align="center"><input type="text" name="username" value="username here" size="32" maxlength="32"></div>
<br>
<div align="center"><input type="text" name="password" value="password here" size="32" maxlength="32"></div>
<br>
<div align="center"><input type="text" name="database" value="database Here" size="32" maxlength="32"></div>
<br>

<div align="center"><input type="submit" name="Submit_button" value="Submit"></div>

</form>

 

What is funky is the page seems to work, it says it writes a record but the record never appears in the database.

I did verify MIME types were correct for HTML to be allowed to write as php if needed, any idea why it wouldn't create a record?

 

Two problems:

 

One you are running the query twice. Once before the condition test and once inside the IF condition test

mysql_query ($query);
if (mysql_query($query))
{
  die('Error: ' . mysql_error());
  }

 

Second, your IF condition is backwards. The way you have, it there will only be an error message if the query successfully completes!

 

Use this:

//mysql_query ($query);
if (!mysql_query($query))
{
  die('Error: ' . mysql_error());
  }

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.