Jump to content

I can't figure out why data isn't saving in my tables


Fish181

Recommended Posts

Hi,

 

I'd like to apologize if I sound like a noob. Other than a book on dummies about php and mysql, I'm new to php and mysql combined.

 

 

Here is the code where my form is:

	<class="form-horizontal"> <!-- this is bootstrap form type -->
		<div id="question"><h1>Have a question?</h1></div>
		
		<div class="well span5">	
			<div class="control-group> <!-- made for each form type -->
			
				<div class="control-label" for="name"><strong>Query</strong></label>
			<div class="controls">
				<!-- start form stuff here -->
				<form action="queries.php" method="post" />
				<input type="text" class="span5" placeholder="Type your question here!" name="user_questions" />
				<input type="submit" value="Submit" class="btn-primary" /></button>
				</form>
				</div>
				</div>
			</div>
		</div>	 

as you can see, the name of the form is "user_questions and the php script linked to it is queries.php.

 

Here is the php code linking mysql to the form:

<?php
error_reporting(-1);


define ('DB_NAME', 'questions');
define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
	die('Could not connect: ' . mysql_error());
	
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
	die('Can\'t use ' . DB_NAME . ':'.mysql_error());
}

echo 'Connected successfully';


$value = $_POST['user_questions'];

$sql = "INSERT INTO questions_user (questions_field) VALUES ('$value')";

if (!mysql_query($sql)) {
	die('Error: ' . mysql_error());
}

mqsql_close();

?>

(yes i'm aware that having root and no password is very dangerous. This website that i'm making is only a project i'm doing to gain more experience.)

 

 

As you can see. The script will die if anything doesn't work and it will give me an error message. However if it successfully gets through linking the database then it will say "successfully connected".

 

 

 

When I put a query into my form and submit, I'm sent to the queries.php page where it says "successfully connected". Since I haven't made any redirecting or anything it just stops there. But then I go to my database to see if the data is stored and it is empty.

 

 

**DATABASE INFO**

 

The db name is "questions"

 

The only table is "questions_user"

 

The 2 fields are "ID", which is the primary key with A I and "questions_field" which stores the data with TEXT.

 

 

 

If you need anything else just let me know. It's driving me insane.

 

Thanks,

 

Fisher 

Link to comment
Share on other sites

I didn't?? How do I do that?

 

It should be something like

$result = mysql_query($sql, $link);

if(!$result) die('Error: ' . mysql_error());

As a newbie in php you should consider using mysqli or pdo libraries in php instead the mysql.

Don't waste your time!

Link to comment
Share on other sites

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.