MySQL row not filling up when form field left empty

I have a HTML form connecting to PHP and that submits it to the database. It works fine when all the fields in the form are filled…however, I put it so all the fields are not required in the HTML(I don’t mind leaving phone, wanted_sal and website blank)… so when the user leaves one field empty, the database doesn’t fill up. Nothing happens. I’m not sure if I need an if statement of sorts?

PHP Code:

// File Upload
	include('conn.php');
	
	$fileinfo=PATHINFO($_FILES["resume"]["name"]);
	$newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
	move_uploaded_file($_FILES["resume"]["tmp_name"],"upload/" . $newFilename);
	$location="upload/" . $newFilename;

	// Application form $POST
	$first_name = $_POST['first_name'];
	$last_name = $_POST['last_name'];
	$birthday = $_POST['birthday'];
	$gender = $_POST['gender'];
	$phone = $_POST['phone'];
	$email = $_POST['email'];
	$wanted_sal = $_POST['wanted_sal'];
	$website = $_POST['website'];

	

  $sql  = mysqli_query($con,"insert into resume (cv_location, first_name, last_name, birthday, gender, phone, email, wanted_sal, website) values ('$location','$first_name', '$last_name', '$birthday', '$gender', '$phone', '$email', '$wanted_sal','$website')");

You are on the right line of thinking, you need to do tests. Take the above snippet. If you are using PHP7.x you can do this:

$website = ($_POST['website'] ?? 'null');
if($website!=='null'){
   $website = '\'' . $website . '\'';
}

But the catch is that you need to not wrap field values in the $sql assignment. You need to selectively wrap based on numeric, string value, or the value null. When you send null to the database, you don’t wrap it.

1 Like

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