I cant insert value in this code plz help

$name=$_REQUEST['name'];
	 $address=$_REQUEST['address'];
	 $email=$_REQUEST['email'];
	 $bloodgroup=$_REQUEST['bloodgroup']; 
	$phone= isset($_GET['phone']) ? $_GET['phone'] : '';
	$lbdate= isset($_GET['lbdate']) ? $_GET['lbdate'] : '';
	$gender= isset($_GET['gender']) ? $_GET['gender'] : '';
	 $link=mysql_connect("localhost","root","") or die("Cannot Connect to the database!");
	
	 mysql_select_db("department",$link) or die ("Cannot select the database!");
	 $query="INSERT INTO `students` (`name`, `address`, `email`, `bloodgroup`, `phone`, `lbdate`, `gender`) values('" . $_POST["name"] . "','" . $_POST["address"] . "','" . $_POST["email"] . "','" . $_POST["bloodgroup"] . "','" . $_POST["phone"] . "','" . $_POST["lbdate"] . "','" . $_POST["gender"] . "')";
		if ($name == '' || $address == ''|| $email == ''|| $bloodgroup == ''|| $phone == ''|| $lbdate == ''|| $gender == '') $error[] = "All fields are required.";

		  if(!mysql_query($query,$link))
		  {die ("An unexpected error occured while saving the record, Please try again!");}
		  else
		 {
		  echo "New record saved successfully!";}

Hi @ishaa027 and welcome to the forums.

When developing I always use these DEBUG settings and make a special point of formatting the script because it is OH, so much easier to debug:

<?php 
declare( strict_types=1 );

define('LOCALHOST', 'localhost'===$_SERVER['SERVER_NAME'] );

# DEFAULTS
	error_reporting(-1);
	ini_set('display_errors','0');  // DO NOT DISPLAY ERRORS
	if( LOCALHOST):
		ini_set('display_errors','1'); //
	endif;

# GET DEFAULTS
	$name       = $_REQUEST['name'];
	$address    = $_REQUEST['address'];
	$email      = $_REQUEST['email'];
	$bloodgroup = $_REQUEST['bloodgroup']; 
	$phone      = isset($GET['phone'])  ? $GET['phone']  : '';
	$lbdate     = isset($GET['lbdate']) ? $GET['lbdate'] : '';
	$gender     = isset($GET['gender']) ? $GET['gender'] : '';

	$query = "
	 	INSERT INTO `students` 
	 	(
	 		`name`, 
	 		`address`, 
	 		`email`, 
	 		`bloodgroup`, 
	 		`phone`, 
	 		`lbdate`, 
	 		`gender`
	 	) 
	 	values
	 	(
	 		'" .$_POST["name"] 		."',
	 		'" .$_POST["address"] 	."',
	 		'" .$_POST["email"] 	. "',
	 		'" .$_POST["bloodgroup"]."',
	 		'" .$_POST["phone"] 	. "',
	 		'" .$_POST["lbdate"] 	. "',
	 		'" .$_POST["gender"] 	. "'
	  )";
	echo '<pre>'; print_r($query); echo '</pre>';

# TESTS
	if (
			$name 			== '' || 
			$address 		== '' || 
			$email 			== '' || 
			$bloodgroup     == '' || 
			$phone 			== '' || 
			$lbdate 		== '' || 
			$gender 		== ''
		) 
		$error[] = "All fields are required.";

# DATABASE STUFF
	if(0):
		$link = mysql_connect("localhost","root","") or die("Cannot Connect to the database!");
		mysql_select_db("department",$link) or die ("Cannot select the database!");
	  if( ! mysql_query($query,$link))
	  {
	  	die ("An unexpected error occured while saving the record, Please try again!");

	  }else{
		  echo "New record saved successfully!";
		}
	endif;	  

I noticed mysql is being used… please do a Google search for PHP MySql

When you say you “can’t insert value in this code”, exactly what do you mean? Which parts of the code work, and which do not?

In the beginning of the code, you use a mixture of $_REQUEST and $_GET for the variables passed into the code. When you build the query, you suddenly switch to using $_POST. Which method do you use when passing the data into the PHP code?

Why do you assign the incoming parameters to variables in the beginning of your code, but not use them in the query?

And as @John_Betong said, the old style mysql calls are no longer part of the current version of PHP, you should look at using mysqli or PDO to access the database. You also need to look at validating the data - you check that none of the fields are empty, but then run the query anyway.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.