Jump to content

Help me save data to database


terryds
 Share

Recommended Posts

I want to save data filled in my form to my MySql database. But, it throws an error..Look at my savedata.php code :

try{$pdo = new PDO('mysql:host=localhost;dbname=mydbname', 'me', 'mypassword');$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);$pdo->exec('SET NAMES "utf8"');}catch (PDOException $e){$error = 'Error connecting to database: ' . $e->getMessage();echo $error;exit();} try{$name = $_POST['name'];$email = $_POST['email'];$msg = $_POST['msg'];$sql = 'INSERT INTO fansSET name = :name,email = :email,message = :msg';$s = $pdo->prepare($sql);$s->bindValue(':name', $name);$s->bindValue(':email', $email);$s->bindValue(':message', $msg);$s->execute();} catch (PDOException $e){$error = 'Error saving data: ' . $e->getMessage();echo $error;exit();} echo 'Succeed to saving your data. Hello, ' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8');?>

My form html code

 <form action="script/savedata.php" method="post"><label for="name">Name</label><br><input type="text" name="name"><br><label for="email">Email</label><br><input type="email" name="email"><br><label for="msg">Message</label><br><textarea name="msg"></textarea><br><input type="submit" name="submit" value="Send!"></form>

The error goes like this :

Error saving data: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Can you help me fix this error ?

Edited by terryds
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...