4 Replies - 385 Views - Last Post: 09 April 2020 - 05:35 AM Rate Topic: -----

#1 pythonhelp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 20-January 19

PHP Insert Record not working

Posted 09 April 2020 - 03:12 AM

I want to insert data into a table, four fields have been captured as parameters from the previous page which display perfectly, the user enters additional information into the form which altogether gets added into the behavior table.

I just don't know why it will not INSERT. After hours, I resorted to some help.

I am, new to PHP.



<?php

$servername = "xxxxx";
$username = "xxxxx";
$password = "xxxx";
$dbname = "xxxxx";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}


$Student_id=$_REQUEST['Student_id'];

$query = "SELECT * from students where Student_id='".$Student_id."'"; 
$result = mysqli_query($conn, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Record Behaviour</title>
<link rel="stylesheet" href="css/style.css" />
<style type="text/css">
.auto-style1 {
	font-family: "Lucida Sans", "Lucida Sans Regular", "Lucida Grande", "Lucida Sans Unicode", Geneva, Verdana, sans-serif;
}
</style>
</head>
<body>
<div class="form">
<h1>Update Record</h1>
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$Student_id=$_REQUEST['Student_id'];
$firstname =$_REQUEST['firstname'];
$lastname =$_REQUEST['lastname'];
$Form_Group =$_REQUEST['Form_Group'];
$Year_Group =$_REQUEST['Year_Group'];
$Incident_Date =$_POST['Incident_Date'];
$Incident_Type=$_POST['Incident_Type'];
$Incident =$_POST['Incident'];
$Recorded_by =$_POST['Recorded_by'];
$Subject =$_POST['Subject'];
$Behaviour_point =$_POST['Behaviour_point'];




 $sql = "INSERT INTO behaviour (student_id, firstname,lastname,Form_Group,Year_Group,Incident_Date, Incident_Type, Incident, Recorded_by, Subject, Behaviour_point)
     VALUES ('$Student_id', '$firstname','$lastname','$Form_Group','$Year_Group', '$Incident_Date', '$Incident_Type','$Incident' ,'$Recorded_by', '$Subject', '$Behaviour_point')";

    
     mysqli_query($conn, $sql) or die(mysqli_error());
     $status = "Record Updated Successfully. </br></br>
     <a href='view.php'>View Updated Record</a>";
     echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>

<div>
<form name="form" method="post" action="add_behaviour.php"> 
<input type="hidden" name="new" value="1" />
	<table style="width: 100%">
		<tr>
			<td>
			&nbsp;</td>
			<td class="auto-style1">
			<input name="Student_id" value="<?php echo $row['Student_id'];?>" class="auto-style1" /></td>
		</tr>
		<tr>
			<td class="auto-style1" style="height: 29px"><strong>Surname</strong></td>
			<td class="auto-style1" style="height: 29px">
			<input name="lastname" type="text" value="<?php echo $row['lastname'];?>"></td>
		</tr>
		<tr>
			<td class="auto-style1"><strong>First Name</strong></td>
			<td class="auto-style1"><input name="firstname" type="text" value="<?php echo $row['firstname'];?>"></td>
		</tr>
		<tr>
			<td class="auto-style1"><strong>Form</strong></td>
			<td class="auto-style1">
			<input name="Form_Group" type="text" value="<?php echo $row['Form_Group'];?>"></td>
		</tr>
		<tr>
			<td class="auto-style1"><strong>Year Group:</strong></td>
			<td><input name="Year_Group" type="text" value="<?php echo $row['Year_Group'];?>"></td>
		</tr>
		<tr>
			<td class="auto-style1">&nbsp;</td>
			<td> &nbsp;</td>
		</tr>
		<tr>
			<td class="auto-style1">Date of Incident</td>
			<td> <input name="Incident_Date" type="date" class="auto-style1" required /></td>
		</tr>
		<tr>
			<td class="auto-style1">Incident Type</td>
			<td> <select name="Incident_Type"class="auto-style1">
	<option selected="">SELECT</option>
	<option value="Missed Assignment">Missed Assignment</option>
	<option value="Inappropriate Behaviour">Inappropriate Behaviour</option>
	</select></td>
		</tr>
		<tr>
			<td class="auto-style1">Details</td>
			<td> 
	<textarea name="Incident" cols="100" rows="4" class="auto-style1" required></textarea></td>
		</tr>
		<tr>
			<td class="auto-style1">Teacher</td>
			<td> <select name="Teacher" class="auto-style1">
	<option selected="">SELECT</option>
	<option value="xxxx">xxx</option>
	<option value="xxxxx">xxxxx</option>
	</select></td>
		</tr>
		<tr>
			<td class="auto-style1" style="height: 27px">Subject</td>
			<td style="height: 27px"> 
			<select name="Subject" class="auto-style1">
			<option>SELECT</option>
			<option>ICT</option>
			<option>COMPUTER SCIENCE</option>
			<
	</select></td>
		</tr>
		<tr>
			<td class="auto-style1" style="height: 27px">&nbsp;</td>
			<td style="height: 27px">&nbsp;</td>
		</tr>
		<tr>
			<td class="auto-style1" style="height: 27px">Behaviour Point</td>
			<td style="height: 27px">
			<input name="Behaviour_point" size="1" type="text" value="1" readonly=""></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td><input name="submit" type="submit" value="Record Incident" /></td>
		</tr>
	</table>
	
</form>
<?php } ?>
</div>
</div>
</body>
</html>





Is This A Good Question/Topic? 0
  • +

Replies To: PHP Insert Record not working

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,571
  • Joined: 08-June 10

Re: PHP Insert Record not working

Posted 09 April 2020 - 03:24 AM

Have you checked that every variable you rely on has the value that you expect it to have?

PS. You are wide open to SQL injection.
Was This Post Helpful? 0
  • +
  • -

#3 pythonhelp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 20-January 19

Re: PHP Insert Record not working

Posted 09 April 2020 - 03:29 AM

I have checked over and over again the textbox names etc and the database field names
Was This Post Helpful? 0
  • +
  • -

#4 pythonhelp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 20-January 19

Re: PHP Insert Record not working

Posted 09 April 2020 - 03:38 AM

OK, yes , it was an error in matching values witjh fields,

Please help me to overcome SQL injections!
Was This Post Helpful? 0
  • +
  • -

#5 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3151
  • View blog
  • Posts: 11,956
  • Joined: 03-December 12

Re: PHP Insert Record not working

Posted 09 April 2020 - 05:35 AM

https://phpdelusions.net/pdo
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1