0

I am having trouble with this code. I want to add a row into the table via a HTML form. Here is the code:

EDIT: Here is the HTML code of the form:

<form role = "form" action = "aanmaken.php" method = "post" class = "col-lg-4 col-lg-offset-1 col-md-8 col-sm-12">
        <div class="form-group">
            <label for "eventName">Naam event: </label>
            <input id = "eventName" class = "form-control" type = "text" name = "eventName" placeholder = "Hier typen">
        </div>
        <div class="form-group">
            <label for "date">Datum: </label>
            <input id = "date" class = "form-control" type = "text" name = "date" placeholder="YYYY-MM-DD">
        </div>
        <div class="form-group">
            <label for "time">Tijd: </label>
            <input id = "time" class = "form-control" type = "text" name = "time" maxlength="5" placeholder="18:00">
        </div>
        <div class="form-group">
            <label for "max_spelers">Max spelers: </label>
            <input id = "max_spelers" class = "form-control" type = "number" name = "max_spelers" maxlength="2" placeholder="Hier typen">
        </div>
        <input id = "submit" type="submit" value="Aanmaken">
    </form>

And the PHP:

include 'connect.php';

$eventName = $_POST['eventName'];
$date = $_POST['date'];
$time = $_POST['time'];
$aantal_spelers = $_POST['max_spelers'];

$addEvent = "INSERT INTO `dbi286018`.`dutch_delight` (`id`, `eventName`, `date`, `time`, `aantal_spelers`, `current_spelers`) VALUES (NULL, $eventName, $date, $time, $aantal_spelers, \'0\');";

if ($conn->query($addEvent) === TRUE) {
    echo "Je hebt een evenement aangemaakt! Vraag op het forum of op xbox live of mensen je evenement willen bijwonen.";
} 
else {
    echo "Er ging iets fout tijdens het aanmaken van je evenement... probeer het later nog eens, of neem contact op met een van de leiders.";
    mysql_error();
}

I keep getting the else statement. Is there something I am doing wrong?

Thanks!

9
  • is $conn created in connect.php?? Commented Oct 28, 2015 at 17:12
  • Yes it is! And it's connected Commented Oct 28, 2015 at 17:15
  • mysql_error()?!? Are you using MySQL? or MySQLi or PDO? Commented Oct 28, 2015 at 17:15
  • add connect.php code here too Commented Oct 28, 2015 at 17:16
  • 1
    Try change to this: $addEvent = "INSERT INTO dbi286018.dutch_delight (id, eventName, date, time, aantal_spelers, current_spelers) VALUES (NULL, '$eventName', '$date', '$time', '$aantal_spelers', '0');"; Commented Oct 28, 2015 at 17:22

1 Answer 1

1

You can try this using PDO because mysql_* functions are deprecated and PDO is more secure:

try {
    $conn = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD);
} catch(PDOException $e) {
    echo $e->getMessage();
    die();
}
$eventName = $_POST['eventName'];
$date = $_POST['date'];
$time = $_POST['time'];
$aantal_spelers = $_POST['max_spelers'];
$sql = "INSERT INTO `dbi286018`.`dutch_delight` (`id`, `eventName`, `date`, `time`, `aantal_spelers`, `current_spelers`) VALUES (:id, :event_name, :date, :time, :aantal_spelers, :current_spelers)";
//Prepare your query
$stmt = $conn->prepare($sql);
//Execute your query binding variables
$stmt->execute(array(':id'=>NULL, ':event_name'=>$eventName, ':date'=>$date, ':time'=>$time, ':aantal_spelers'=>$aantal_spelers, ':current_spelers'=>0));
Sign up to request clarification or add additional context in comments.

2 Comments

Works as well! Thanks a lot!
You can find a nice tutorial about PDO here: phpro.org/tutorials/Introduction-to-PHP-PDO.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.