Reputation: 3351
I'm very new to php and this is my first project on this. Here I'm having a simple html form that submit data to mySql database.
I've created a table in mySql with 3 columns, 1st JobId
which is an auro incriment field and primary key. 2nd and 3rd are Title
, JobDescription
which are varchars. In my html I fill the data and submit and the data should be posted in the table that I've created.
Here is my HTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Post Job</title>
</head>
<body>
<form action="jobSubmit.php" method="Post">
<input type="text" name="jTitle" placeholder="Enter Title" required/>
<textarea name="jDesc" placeholder="Enter Job Description" required></textarea>
<button type="submit" value="Submit"></button>
</form>
</body>
</html>
and here is my php code.
<?php
$jTitle = $_POST['jTitle'];
$jDesc = $_POST['jDesc'];
if (!empty($jTitle) && !empty($jDesc))
{
$host = "localhost";
$dbUserName = "muUserId";
$dbPassword = "myPwd";
$dbname = "Jobs";
try
{
$conn = new mysqli($host, $dbUserName, $dbPassword, $dbname);
}
catch(Exception $ex)
{
echo 'Error : ' . $ex->getMessage();
}
if (mysqli_connect_error())
{
die('Connect Error(' . mysqli_connect_errno() . ')' . mysqli_connect_error());
}
else
{
echo $jDesc . ' and ' . $jTitle;
//$SELECT = "SELECT JobId from OpenPositions Where JobId=? LIMIT 1";
$INSERT = "INSERT into OpenPositions (Title, JobDescription) values (?,?)";
$stmt = $conn->prepare($INSERT);
$stmt . bind_param("s,s", $jTitle, $jDesc);
try
{
$stmt . execute();
echo "New Record Inserted";
}
catch(Exception $ex)
{
echo 'Error : ' . $ex->getMessage();
}
$stmt->close();
$conn->close();
}
}
else
{
echo "Enter both Title and Desription";
die();
}
?>
When I run it, enter Test title
and Tse Desc
and hit submit, I get the below screen.
And another point I missed to update. I'm hosting this on godaddy.
Please let me know where I'm going wrong and how can I fix this.
Thanks
Update 1
here is my schema.
Upvotes: 0
Views: 359
Reputation: 67
Check the PHP error log file. You can found error log file location in php.ini, error_log="/path/to/error/logs".
or
place the
error_reporting(E_ALL);
at the top of php code, don't forget to remove it after the error is solved. This will display error on you page instead of return error server 500.
Upvotes: 0
Reputation: 685
#Correct code is :
<?php
$jTitle = $_POST['jTitle'];
$jDesc = $_POST['jDesc'];
if (!empty($jTitle) && !empty($jDesc))
{
$host = "localhost";
$dbUserName = "root";
$dbPassword = "";
$dbname = "Jobs";
try
{
$conn = new mysqli($host, $dbUserName, $dbPassword, $dbname);
}
catch(Exception $ex)
{
echo 'Error : ' . $ex->getMessage();
}
if (mysqli_connect_error())
{
die('Connect Error(' . mysqli_connect_errno() . ')' . mysqli_connect_error());
}
else
{
echo $jDesc . ' and ' . $jTitle;
//$SELECT = "SELECT JobId from OpenPositions Where JobId=? LIMIT 1";
$INSERT = "INSERT into OpenPositions (Title, JobDescription) values (?,?)";
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("ss",$jTitle,$jDesc);
try
{
$stmt->execute();
echo "New Record Inserted";
}
catch(Exception $ex)
{
echo 'Error : ' . $ex->getMessage();
}
$stmt->close();
$conn->close();
}
}
else
{
echo "Enter both Title and Desription";
die();
}
?>
#What wrong in your code :
bind_param() is a method of your $stmt object so you need to use it like $stmt->bind_param() insteadof $stmt.bind_param() | also $stmt.execute() will be $stmt->execute()
bind_param() first parameter about input type should be like 'ss' insteadof 's,s'
Take a tour for clear knowledge of bind_param() https://www.php.net/manual/en/mysqli-stmt.bind-param.php
Live Demo
Upvotes: 1