Jump to content

Recommended Posts

Hey,

 

I'm having trouble getting information from my form and inserting it into a MySQL database.

 

This is my form. (I wanted to do something really simple first to make sure it works before trying it on a more complicated form)

<form method="post" action="process.php">
Name:<br>
<input type="text" name="user_name">
<br>
Email: <br>
<input type="text" name="user_email">
<br><br>
<input type="submit" name="Submit" value="Submit">
</form>

 

my process.php code

<?php
require("./connection.php");

$user_name = check_input($_POST['user_name']);
$user_email = check_input($_POST['user_email']);

function check_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>

<html>
<body>
<?php
$sql = "INSERT INTO user_info (user_name, user_email) VALUES ('{$user_name}', '{$user_email}')";
if($result = mysql_query($sql) {
echo '<h1>Thank you</h1>Your information has been entered into our database';
} else {
echo "ERROR: ".mysql_error();
}

?> 

</body>
</html>

 

Can anyone help with this? I'd much appreciate it.

Link to comment
https://forums.phpfreaks.com/topic/192821-php-form-insert-into-mysql-db/
Share on other sites

Since you did not provide any information as to what it does do when you tried it, no one can directly help you with the dozen possible things that could prevent it from working on your server. What result did you see in front of you when you submitted the form?

Sorry, it was originally just giving me a blank page and nothing was going into the table in the database.

 

Sorry for the late reply was kind of expecting an email so I knew when I got a response but it seems I forgot to tick the box for that.

 

Anyway, I've got it working now with some googling and changing of code but I had to replace "require("./connection.php");" with some script that connects to the database in the page itself.

 

Any idea as to why it would work without "require("./connection.php");"? It works on my other pages. Here's the code from the connection.php file:

<?php

// Connect to the database
$conn = new MySQLi("localhost", "username", "pass", "dbname") 
or die('Could not connect: ' . mysql_error());

?>

 

edit: Here's what I', using now:

<?php

$username="username";
$password="pass";
$database="dbname";

//create shorthand for form data
$game_id = $_REQUEST['game_id'];
$review_title = $_REQUEST['review_title'];
$review_text = $_REQUEST['review_text'];
$graphics = $_REQUEST['graphics'];
$gameplay = $_REQUEST['gameplay'];
$sound = $_REQUEST['sound'];
$replayability = $_REQUEST['replayability'];
$pros = $_REQUEST['pros'];
$cons = $_REQUEST['cons'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");


$query = "INSERT INTO xreview VALUES('', '$game_id','$review_title', '$review_text', '$graphics','$gameplay','$sound','$replayability','$pros','$cons')";
$result = MYSQL_QUERY($query); 

mysql_close();
?>

I've made sure that it's in the directory that I've told it to look in but now I'm getting a page.

 

However, now I'm getting the page but nothing is going into the database. I am able to echo out the information contained in the variables that should be going into the database so the information is getting into the variables it's just not going into the table I need it to go in.

 

I don't really see much of a difference between the connection.php file:

// Connect to the database
$conn = new MySQLi("localhost", "kahmed14", "240985", "kahmed14") 
or die('Could not connect: ' . mysql_error());

 

and

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

 

They're both doing the same thing aren't they?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.