Unable to insert data in mysql using php

i know this is a beginner's question .I am working on a bloodbank database project with html,php and mysql. Here as an administrator,i am trying to send messages to users.At first i am trying to see if the user with the username is present in the database.if he is present i am inserting the username and messages into the table called usermessages But i am not able to insert the data.i am getting the message "message sent successfully",but in reality it is not getting updated in the database.So here is my code,i can assure all that no spelling mistake is present in database or in phpcode.

<?php
session_start();
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="bloodbank"; // Database name 
$tbl_name="users"; // Table name 
$tblname="usermessages"; 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and messages is sent from form 
$username=$_POST['username']; 
$sql="SELECT * FROM $tbl_name WHERE username='$username'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

if($count==1)
{
$mysql="INSERT INTO tblname(username, messages)
VALUES
('$_POST[username]','$_POST[messages]')";
echo "Message Sent Successfully";
}
else
{
echo "No user with that username found in the database";
}

?>

asked May 31, 2013 at 5:51

1

Try to execute the query

$mysql="INSERT INTO $tblname(username, messages)
VALUES ('$_POST[username]','$_POST[messages]')";
$return = mysql_query($my_sql);
echo "Message Sent Successfully";

You just forgotted to execute this insert query

And my advice is dont use mysql_* functions as they are depricated,use either mysqli_* functions or PDO Statements,and while you are playing with the post variables try to escape them like

mysql_real_escape_string($_POST['messages']);

answered May 31, 2013 at 5:54

Unable to insert data in mysql using php

GautamD31GautamD31

28.2k10 gold badges60 silver badges84 bronze badges

4

Your query is good but you haven't executed it. Use mysql_query to execute your query.

Second please be careful about sql injection. Your code is shouting that come and hack me.

answered May 31, 2013 at 6:04

Unable to insert data in mysql using php

Awais QarniAwais Qarni

16.7k23 gold badges74 silver badges134 bronze badges

Not the answer you're looking for? Browse other questions tagged php html mysql or ask your own question.

This is my first post on Daniweb.

I am developing a social networking site and the register form isn't quite working as expected.


Here is the PHP code :

<? $title = 'Register' ?>
<?require ('scripts/top.php'); ?>

<link rel='stylesheet' type='text/css' href='scripts/form.css'/>

<div id='content'> 

<h2> Register an account </h2>

<div id='form'>


<?

//Main form
$form = "

<div id ='message'>Fields marked with <font color='#A61616'>*</font> are required</div>

<form action='register.php' method='POST'>

	<table cellspacing='8px'>
	
		<tr>
			<td>
				First name <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='text' name='firstname' class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Last name <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='text' name='lastname'class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Username <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='text' name='username'class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Gender <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='radio' name='gender' value='Male' class='radio'/> Male
				<input type='radio' name='gender' value='Female'class='radio'/> Female
			</td>
		</tr>
		
		<tr>
			<td>
				Email <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='text' name='email'class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Password <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='password' name='pass'class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Confirm password <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='password' name='repass'class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Relationship status
			</td>
			<td>
				<select name='relation' class='list'>
				<option>Select your relationship status</option>
				<option value=\"Single\">Single</option>
				<option value=\"In a relationship\">In a relationship</option>
				<option value=\"Married\">Married</option>
				</select>
			</td>
		</tr>
		
		<tr>
			<td>
				Here for
			</td>
			<td>
				<select name='interest' class='list'>
				<option>Select what you are interested in</option>
				<option value=\"Making friends\">Making friends</option>
				<option value=\"Networking\">Networking</option>
				<option value=\"Fun\">Fun</option>
				<option value=\"Other\">Other</option>
				</select>
			</td>
		</tr>
		
		<tr>
			<td>
				Display Picture
			</td>
			<td>
				<input type='file' name='dp'/>
			</td>
		</tr>
		
		<tr>
			<td>
				About
			</td>
			<td>
				<textarea name='about' rows='5' cols='25' class='textbox'></textarea>
			</td>
		</tr>
		
		<tr>
			<td>
				Hobbies
			</td>
			<td>
				<textarea name='hobbies' rows='3' cols='25' class='textbox'></textarea>
			</td>
		</tr>
		
		<tr>
			<td>
			</td>
			<td>
				<input type='submit' name='submitbtn' value='Register' class='button'/>
			</td>
		</tr>
		
	</table>
	
</form>
";

// Checking for entered values and assigning variables

if (isset($_POST['submitbtn'])) {

$firstname = strip_tags($_POST['firstname']);
$lastname = strip_tags($_POST['lastname']);
$username = strip_tags($_POST['username']);

if ($_POST['gender']){
$gender = strip_tags($_POST['gender']);
}
else {$gender = 'Not specified';}

$email = strip_tags($_POST['email']);
$pass = strip_tags($_POST['pass']);
$repass = strip_tags($_POST['repass']);

if ($relation){
$relation = strip_tags($_POST['relation']);
} else {$relation = '&nbsp;';}

if ($interest){
$interest = strip_tags($_POST['interest']);
} else {$interest = '&nbsp;';}

if ($about){
$about = strip_tags($_POST['about']);
} else {$about = '&nbsp;';}

if ($hobbies) {
$hobbies = strip_tags($_POST['hobbies']);
} else {$hobbies = '&nbsp;';}

if ($_POST['dp']) {

$name = $_FILES['dp']['name'];
$type = $_FILES['dp']['type'];
$size = $_FILES['dp']['size'];
$tmpname = $_FILES['dp']['tmpname'];
$ext = substr($name, strpos($name,'.'));
} 


// The real registration process begins

// Checking to see if all required fields have been entered

	if ($firstname && $lastname && $username && $gender && $email && $pass && $repass) {

// Checking if both entered passwords match

			if ($pass == $repass){

			
// Checking if the email address entered is valid
			
				if (strstr($email, '@') && strstr($email, '.') && strlen($email)>=6) {
				
// Connecting to database by requiring 'connect' file
								
				require ("scripts/connect.php");
				
				
				
// Checking if username already exists
								
				$query = mysql_query("SELECT * FROM members WHERE username='$username'");
				$num_rows = mysql_num_rows ($query);
				
						if ($numrows == 0) {
			

			
// Checking if e-mail address already exists
						
								$query = mysql_query("SELECT * FROM members WHERE email='$email'");
								$num_rows = mysql_num_rows ($query);
								
										if ($numrows == 0) {
										
										
// Encrypting password					
										$password = md5(md5($pass));
										$date = date("F d, Y");
								
// Uploading avatar								
											if ($name) {
											move_uploaded_file($tmpname, "avatars/$username.$ext");
											$dp = "$username.$ext";
											}
											
											else{
											
											if ($gender == "Male") {$dp = "images/defaultavatars/male.png";}
											else
											if ($gender == "Female") {$dp = "images/defaultavatars/female.png";}
											
											}

// Generating random code
											
											$code = substr(md5(mt_rand(111111111,9999999999)), 2, 20);
											


// Inserting values
											
											"INSERT INTO `members` ('id','first_name','last_name','username','email','password,'gender','about','hobbies','looking_for','relationship_status','dp','last_visit','active','code','blocked','date','showemail','friends')
														 VALUES('','$firstname','$lastname','$username','$email','$password,'$gender','$about','$hobbies','$interest','$relation','$dp','','0','','0','$date','0','')";
											
																					
											
// Sending email for activation
											
											$webmaster = "My email goes here";
											$subject = "Activate your account on My sitename";
											$headers = "From: My sitename<$webmaster>";
											$message = "Hey $firstname! Thank you for registering on My sitename. Please follow the link below to activate your account: 
														http://mysite/activate.php?code=$code If the above link does not work, go to http://mysite/activate.php and type in this code: $code ";
																						
											
											mail($email, $subject, $message, $headers);
											

// Giving success message											
											
											echo "<div id = 'success'> <b>Thanks for registering!</b> <br/> You will receive an email with a link to activate your account.<br/> Your email address is $email <br/> <br/> <i>No email? Check your <b>spam</b> folder. </i> <br/> Still having problems? <a href='contact.php'> Contact us </div>";
											
											
											
											
											
											}
										
								
								else {echo "<div id='error'> That e-mail address is already taken </div>";
									  echo $form;}
						
				
				}
				
				else {echo "<div id='error'> That username is already taken </div>";
					  echo $form;}
				
				
				
				
// Error on incorrect email
				
				}
				
				else {echo "<div id='error'> Invalid email address entered </div>";
					  echo $form;}
			
			
// Error on passwords not matching
			
			}
			else {echo "<div id='error'> Your passwords do not match </div>";
				  echo $form;}

}
// Error on not entering all fields


	
		else {echo "<div id='error'> Please fill all the required fields </div>";
		echo $form ;}

}


// If the submit button has not yet been pressed, the form will be echoed

else {
	echo $form;
}


?>

</div>

</div>

<?require ('scripts/bottom.php'); ?>

:S The script almost works fine. I even get the success message. But when I check into my database table called 'members' using PHPMyAdmin, there is just no row created :icon_cry:

Here is how my 'Connect.php' file is set up :

<?php

$server = 'myservername';
$dbuser = 'mydatabaseusername';
$dbpass = 'mydatabasepassword';
$database = 'mydatabasename';

$con = mysql_connect ($server, $dbuser, $dbpass);
mysql_select_db ($database, $con);

?>

Any help would be appreciated

How to insert data in PHP to MySQL?

The INSERT INTO statement is used to add new records to a MySQL table: INSERT INTO table_name (column1, column2, column3,...).
The SQL query must be quoted in PHP..
String values inside the SQL query must be quoted..
Numeric values must not be quoted..
The word NULL must not be quoted..

Why data is not inserting in MySQL?

try changing your line to this: mysql_query("INSERT INTO data VALUES ('$username', '$password')") or die(mysql_error()); You're catching errors for selecting and connecting the database but not the insert. You haven't passed column names in your insert query, so make sure you have only two columns in data table.

Can not connect to MySQL in PHP?

PHP may not be able to connect to the MySQL server if the server name is not recognized. Make sure that the server name is set to localhost. In case of other errors, make sure to consult the error_log file to help when trying to solve any issues. The file is located in the same folder where the script is running.

How do you insert data into MySQL?

To insert data into a MySQL table, you would need to use the SQL INSERT INTO command. You can insert data into the MySQL table by using the mysql> prompt or by using any script like PHP.