0

Can you help me?

The data that I input in my form can't save in my database, but I don't have error in my codes.

Here's my codes:

<body>
<?php
    require('dbconevent.php');

         if(isset($_POST['submit'])){
          $eventname = addslashes($_POST['event_name']); //get values
          $register_name = addslashes($_POST['register_name']); 
          $register_email = addslashes($_POST['register_email']);
          $register_number = addslashes($_POST['register_number']);
          $sql = "Insert into register(event_name, register_name, register_email, register_number) values ('".$eventname."', '".$register_name."',  '".$register_email."', '".$register_number."');";
           echo $sql;return; 
           $res = mysqli_query($con,$sql); //res for result just a variable name, mysqli_query() returns true or false value upon excecuting sql
           if($res){ //if sql [ inserted ] is true
              echo 'Added!';
             }else{
              echo 'Something Went Wrong'; //Sql error or db error
            }
    }

?>
<script>
  alert('You are registered!');
  window.location = 'event.php';
</script>
</body>

I hope someone can help me. Thank you

  • remove this line `echo $sql;return;` – Sahil Gulati Apr 27 '17 at 17:10
  • 1
    Your code is vulnerable to [**SQL injection attacks**](https://en.wikipedia.org/wiki/SQL_injection). You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 27 '17 at 17:10
  • You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Use [`mysqli_error()`](http://php.net/manual/en/mysqli.error.php) to get a detailed error message from the database. – John Conde Apr 27 '17 at 17:12
  • *"but i don't have error"* - But I don't see *any* error checking/handling. You might once you get rid of that return - http://php.net/manual/en/function.return.php - *"If called from within a function, the **return statement immediately ends execution** of the current function"*. - Edit: This being the *real* reason. – Funk Forty Niner Apr 27 '17 at 17:13
  • @SahilGulati Thank you very much! You're a great help! – pandadedidodu Apr 27 '17 at 17:14
  • @pandadedidodu welcome .... :) – Sahil Gulati Apr 27 '17 at 17:14

0 Answers0