Why is my INSERT not working?

Post here if you need help with SQL.
Post Reply
User avatar
Linkjuice57
Posts: 23
Joined: Tue Jul 12, 2011 2:44 pm

Why is my INSERT not working?

Post by Linkjuice57 »

Hi,

I've been struggling on this for a couple of hours now, why is my INSERT not working? I've done similair things before, and I figure i'm missing something pretty obvious. Can someone point it out?

add.php
[syntax=php]<?php include '../includes/connect.php'; ?>



<?php
if($_POST['coupon']){

$coupon = $_POST['coupon'];
$waarde = $_POST['waarde'];
$datum = $_POST['datum'];

$insert = "INSERT INTO adwords (coupon, waarde, datum) VALUES ('$coupon', '$waarde', '$datum ')" or die(mysql_error());
echo "Succes. <br />";
echo "<a href=\"index.php\">Terug</a>";

}
?>[/syntax]

index.php
[syntax=php]<?php include 'add.php'; ?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" lang="nl">
<title>/Adwords/</title>
<style>
h1, h2 { margin:0px; padding:0px; }
body { margin:120px auto 0 auto; width:960px; }
label { width:100px; float:left; }
</style>
</head>

<body>

<h1>Adwords Coupon Advertentietegoed</h1>



<form action="add.php" method="post">
<label for="coupon">Couponcode: </label><input type="text" name="coupon" /><br />
<label for="waarde">Waarde: </label><input type="text" name="waarde" /><br />
<label for="datum">Geldig t/m: </label><input type="text" name="datum" /><br />
<input type="submit" name="submit" id="submit" />
</form>

<?php

$query = mysql_query("SELECT * FROM adwords") or die(mysql_error());


?>

<table border="1">
<tr>
<td>Couponcode</td>
<td>Waarde</td>
<td>Geldig tot</td>
<td>&nbsp;</td>
</tr>

<?php while($row = mysql_fetch_array($query)) { ?>

<tr>
<td><?= $row['coupon']; ?></td>
<td>&euro; <?= $row['waarde']; ?>,-</td>
<td><?= $row['datum']; ?></td>
<td><a href="">Verbruikt?</a></td>
</tr>

<? } ?>
</table>

</body>
</html>[/syntax]

Table is being made, and echo's stuff from the DB just fine, but I can't seem to add anything new to it.
non-existent PHP newb
JelvinJS7
Posts: 341
Joined: Thu May 12, 2011 8:40 pm

Re: Why is my INSERT not working?

Post by JelvinJS7 »

You're storing the insert code into a variable, and not calliing it. You should either initially run the INSERT, or out the code in a variable, then run it (which isn't that helpful).

[syntax=php]
       mysql_query("INSERT INTO adwords (coupon, waarde, datum) VALUES ('$coupon', '$waarde', '$datum ')") or die(mysql_error());

[/syntax]

Or

[syntax=php]
        $insert = "INSERT INTO adwords (coupon, waarde, datum) VALUES ('$coupon', '$waarde', '$datum ')";
mysql_query($insert) or die(mysql_error());

[/syntax]
User avatar
Linkjuice57
Posts: 23
Joined: Tue Jul 12, 2011 2:44 pm

Re: Why is my INSERT not working?

Post by Linkjuice57 »

Thank you very much Jelvin.

Silly me.
non-existent PHP newb
Post Reply