Noob PHP Question - MySQL

Status
Not open for further replies.

Gillette

[H]ard|Gawd
Joined
Feb 6, 2005
Messages
1,317
I'm just starting out with PHP and I've got a very basic test right now...

I've got two HTML forms defined in the following page, this page also displays the current data contained in the table:

Code:
<?
include 'main/security.php';

//connect 
$db = mysql_connect($dbhost,$dbuser,$dbpass); 
mysql_select_db("$dbname",$db);

$sql = "SELECT * FROM test";
$result = mysql_query($sql);
$fields_num = mysql_num_fields($result);

echo "<h1>Testing Results: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);

?> 


<html>
<body>

<form action="insert.php" method="post">
text: <input type="text" name="text" />
info: <input type="text" name="info" />

<input type="submit" />
</form>

</body>
</html>

When this form is submitted it calls the following page:

Code:
<?php

include 'main/security.php';

$db = mysql_connect($dbhost,$dbuser,$dbpass); 
mysql_select_db("$dbname",$db);

$sql="INSERT INTO test (text)
VALUES
('$_POST[text]')";

$test="INSERT INTO test (info)
VALUES
('$_POST[info]')";

if (!mysql_query($sql,$db))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($db)
?>

<html>
<a href="test.php">Click here to go back to the main page</a>
</html>

What I'm seeing is that only the first value (inserted through $sql) is being written to the database.

Any ideas why?
 
Nevermind - realized the errors of my ways.

Only calling $sql when I do the mysql_query.
 
Status
Not open for further replies.
Back
Top