user2574794
user2574794

Reputation: 1006

Basic prepared statement not INSERTING

I am for the first time trying to build a simple form to insert data to a MySQL table. It is not working however.

This is the form:

<form action="" method="post">
Select Park : <select name="park_id">
              <option value="">Select Park</option>

              <?php foreach ($res as $row) {

              printf('<option value="%s">%s</option>' . PHP_EOL, $row['park_id'], $row['name']);

               } ?>

    </select>

Headline: <input type="text" name="headline">
<br>
Story: <input type="textbox" name="story">
<br>
Date: <input type="date" name="date">

Password :<input type="password" value="" name="password" id="password">

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

and this is the php:

if(isset($_POST['submit'])){

$stmt = $db->prepare("INSERT INTO tpf_newstest(headline,story,date,park_id) VALUES(:headline,:story,:date,:park_id)");
$stmt->execute(array(':headline' => $headline, ':story' => $story, ':date' => $date, ':park_id' => $park_id));
$affected_rows = $stmt->rowCount();
}

It it all within the same index.php file. Any ideas as to what I've done wrong? Thanks

EDIT: if it helps this is the full page

try
{
    $pdo = new PDO('mysql:host=localhost;dbname=*', '*','******');

    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec('SET NAMES "utf8"');
}
    catch (PDOException $e)
{
    $output = 'Unable to connect to the database server.';
    include 'output.html.php';
    exit();
}

$output = 'Theme Park Database initialized';


$qry = "select park_id, name from tpf_parks order by name";
$res = $pdo->query($qry);


if(isset($_POST['submit'])){

$stmt = $db->prepare("INSERT INTO tpf_newstest(headline,story,date,park_id) VALUES(:headline,:story,:date,:park_id)");
$stmt->execute(array(':headline' => $headline, ':story' => $story, ':date' => $date, ':park_id' => $park_id));
$affected_rows = $stmt->rowCount();
}

?>

<form action="" method="post">
Select Park : <select name="park_id">
              <option value="">Select Park</option>

              <?php foreach ($res as $row) {

              printf('<option value="%s">%s</option>' . PHP_EOL, $row['park_id'], $row['name']);

               } ?>

    </select>

Headline: <input type="text" name="headline">
<br>
Story: <input type="textbox" name="story">
<br>
Date: <input type="date" name="date">

Password :<input type="password" value="" name="password" id="password">

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

Upvotes: 0

Views: 75

Answers (2)

DevZer0
DevZer0

Reputation: 13525

There is no name for submit. so how can if(isset($_POST['submit'])){ become true? you can adjust the html as follows and try

<input type="submit" name="submit" value="Submit" />

Upvotes: 1

Hanfeng
Hanfeng

Reputation: 635

I think the problem is that there is no field named submit at all in the $_POST, so the code of inserting will not be executed. To fix this problem, instead of check submit field, you may check other fields, such as park_id, date and so on.

Hope helps!

Upvotes: 2

Related Questions