I am doing simple form that inserts values in database table this is the code
new_subject,php
<form action="create_subject.php" method="post">
<p>Menu name:
<input type="text" name="menu_name" value="" />
<p>Position:
<select name="position">
<?php
$subject_set= find_all_subjects();
$subject_count=mysqli_num_rows($subject_set);
for($count=1; $count<=$subject_count+1; $count++){
echo "<option value='{$count}' >{$count}</option>";
}
?>
</select>
</p>
<p>Visible
<input type="radio" value="0" name="visible" />No
<input type="radio" value="1" name="visible" />Yes
</p>
<input type="submit" value="Submit" name="submit" />
<br/> <br/>
<a href="manage_content.php">Cancel</a>
</form>
create_subject.php
<?php
if(isset($_POST['submit'])){
$menu_name= $_POST["menu_name"];
$position = (int) $_POST["position"];
$visible= (int)$_POST["visible"];
$query="INSERT INTO subjects (menu_name , position , visible) VALUES($menu_name, $position, $visible)";
$result= mysqli_query($connection, $query);
if($result){
header("Location : manage_content.php");
exit();
}
else {
echo 'There was some problem ' ;
echo mysqli_error($connection);
}
}
else {
header('Location: new_subject.php');
exit();
}
?>
Now this is the error There was some problem unknown field 'name' // name is the value that i enter in text box. Will someone please help me about the issue. if i dont enter value for text box other values are successfully entered in database
put single quotes around the values it will fix the issue
$query="INSERT INTO subjects (menu_name , position , visible) VALUES('$menu_name', '$position', '$visible')";
</div>
As a hint to make easy to read and maintainable sql statements use sprintf():
$query= sprintf("INSERT INTO subjects (menu_name , position , visible) VALUES('%s', '%s', '%s')",$menu_name, $position, $visible);
or
$sql = "INSERT INTO subjects (menu_name , position , visible) VALUES('%s', '%s', '%s')";
$query = sprintf($sql, $menu_name, $position, $visible);