-3

im trying to get my code to work but its not, i made two forms with update, delete and add buttons to get data out of my database, Vrienden is the table and Voornaam, Achternaam, Omschrijving are the fields. the update and delete buttons work just fine, im having proplems with my add button. can someon help me. code

<body>

<?php
include('menu.html');
require('db_connectie.php');

if(isset($_POST['update'])) {
$UpdateQuery = "UPDATE Vrienden SET Voornaam='$_POST[voornaam]', Achternaam='$_POST[achternaam]', Omschrijving='$_POST[omschrijving]' WHERE ID='$_POST[hidden]'";
mysql_query($UpdateQuery, $conn);
}

if(isset($_POST['delete'])) {
$DeleteQuery = "DELETE FROM Vrienden WHERE ID='$_POST[hidden]'";
mysql_query($DeleteQuery, $conn);
}

$voornaam = mysql_escape_string($_POST["addvoornaam"]);
$achternaam = mysql_escape_string($_POST["addachternaam"]);
$omschrijving = mysql_escape_string($_POST["addomschrijving"]);

if(isset($_POST['add'])) {
//$AddQuery = "INSERT INTO Vrienden (Voornaam, Achternaam, Omschrijving) VALUES ('$voornaam', '$achternaam', '$omschrijving')";
//$AddQuery("INSERT INTO Vrienden (Voornaam, Achternaam, Omschrijving) VALUES ('".$_POST['addvoornaam']."', '".$_POST['addvoornaam']."', '".$_POST['addomschrijving']."')");
$AddQuery = "INSERT INTO Vrienden (Voornaam, Achternaam, Omschrijving) VALUES ('" . $_POST['addvoornaam'] . "', '" . $_POST['addachternaam'] . "', '" . $_POST['addomschrijving'] . "')";
mysql_query($AddQuery, $conn);
}


$query = "SELECT * FROM Vrienden";
$result = mysql_query($query,$conn);
echo "<table border=0>";
echo "<tr>";
    echo "<th>Voornaam</th>";
    echo "<th>Achternaam</th>";
    echo "<th>Omschrijving</th>";
echo "</tr>";


while($record = mysql_fetch_array($result)) {
echo '<form action=friends.php method=post>';
echo '<tr>';
echo '<td>' . '<input type="text" name="voornaam" value="' . $record['Voornaam'] . '">'.'</td>';
echo '<td>' . '<input type="text" name="achternaam" value="' . $record['Achternaam'] . '">'.'</td>';
echo '<td>' . '<input type="text" name="omschrijving" value="' . $record['Omschrijving'] . '">'.'</td>';
echo "<input type=hidden name=hidden value=" . $record['ID'] . ">";
echo '<td>' . '<input type=submit name=update value=Wijzigen > </td>';
echo '<td>' . '<input type=submit name=delete value=Verwijderen > </td>';
echo '</tr>';
echo '</form>';
}
echo '<form action=friends.php method=post';
echo '<tr>';
echo '<td><input type=text name= addvoornaam></td>';
echo '<td><input type=text name= addachternaam></td>';
echo '<td><input type=text name= addomschrijving></td>';
echo '<td><input type=submit name=add value=Toevoegen></td>';
echo '</form>';
echo '</table>';


mysql_close($conn);
?>
</body>
</html>

here is my code, the update and delete buttons work fine, but the add one isn't working. ive tried multiple solutions. is it a simple syntax problem im not seeing?

  • Consult these following links http://php.net/manual/en/function.mysql-error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code. – Funk Forty Niner Dec 22 '15 at 17:20
  • 2
    **Code Full Of Error.** *I'm Shocked To Know That Delete & Update Button Works Fine* – Nana Partykar Dec 22 '15 at 17:20
  • Use the variables that you set to `mysql_escape_string` in the `INSERT` query, not the original POST variables. – Barmar Dec 22 '15 at 17:29
  • Give me one minute with this web page and [I will erase your entire database](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Also, your database library has been unsupported for 5 years, [and won't even work in the current version of PHP](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). Stop using it. – miken32 Dec 22 '15 at 19:35

1 Answers1

1

See, as you said "the update and delete buttons work just fine". So, I'm not entering into it.

Change here to make your add button work too.

Change

echo '<form action=friends.php method=post';

to

echo '<form action=friends.php method=post>';

Missing closing > in end of <form> for add button.

echo '<form action=friends.php method=post>';
                                          ^Missing Here.

Updated Code.

echo '<form action=friends.php method=post>';
echo '<tr>';
echo '<td><input type=text name= addvoornaam></td>';
echo '<td><input type=text name= addachternaam></td>';
echo '<td><input type=text name= addomschrijving></td>';
echo '<td><input type=submit name=add value=Toevoegen></td>';
echo '</tr>';
echo '</form>';
echo '</table>';

[NOTE: Don't Use Mysql* Functions. They Are Deprecated. Use Mysqli* Or Prepared Statement To Execute Query ]

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77