Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys! I am trying to insert data to the database and after sending the data using form the URL which I am using POST method will be something like this " somelinks/add-result.php?"
What's going on with it, any idea?

What I have tried:

this is the page for inserting data to the database, and I am using form action as insert.php and method is POST.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "result1";
$con= mysqli_connect('localhost','root','','$dbname');

if(!$con){
echo'Not connected to server';
}
if(!mysqli_select_db($con,'result1'))
{
echo 'data base not selected';
}
$name =$_GET['Name'];
$id =$_GET['id'];
$Class =$_GET['Class'];
$Law =$_GET['Law'];
$Hoqoq =$_GET['Hoqoq'];
$PanelCode =$_GET['Law panel'];

$sql="INSERT INTO result (Name,id,Class,Hoqoq,Law,Law panel)VALUES('$name','$id','$Class','$Hoqoq','$Law','$PanelCode')";
if(!mysqli_query($con,$sql))
{
echo'Not Inserted';
}
else{
echo'Inserted';
}
header("refresh:2; url=admin-panel.php");
?>
Posted
Updated 13-Apr-21 22:41pm
Comments
Richard Deeming 5-Oct-20 10:04am    
$sql="INSERT INTO result (Name,id,Class,Hoqoq,Law,Law panel)VALUES('$name','$id','$Class','$Hoqoq','$Law','$PanelCode')";

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation / interpolation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
Afg Hunter 5-Oct-20 12:20pm    
its hard to digest those things written in manual but I will have a research on it, Thanks Richard! <3
Afg Hunter 5-Oct-20 12:22pm    
do you think this one is fine ?

connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// prepare and bind
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email);

// set parameters and execute
$firstname = "John";
$lastname = "Doe";
$email = "john@example.com";
$stmt->execute();

$firstname = "Mary";
$lastname = "Moe";
$email = "mary@example.com";
$stmt->execute();

$firstname = "Julie";
$lastname = "Dooley";
$email = "julie@example.com";
$stmt->execute();

echo "New records created successfully";

$stmt->close();
$conn->close();
?>
Richard Deeming 5-Oct-20 12:23pm    
That looks much better. :)
Afg Hunter 6-Oct-20 3:11am    
then how do I take the data from HTML form and send it to data base ?

The data sent in a POST request is in the $_POST array. See PHP: $_POST - Manual[^]
 
Share this answer
 
Adding to Sandeep's answer, above, there's a third option:

$_REQUEST works for both $_PUT and $_GET data. There's a possible catch: if you post both types of data to the same page AND they both use the same id then you will have a clash in not being able to get them both. HOWEVER - I've never done both, together, and certainly wouldn't reuse the name for the index, anyway. It's never been a problem in about ten years.


 
Share this answer
 
v2
You say that you POST the data but in your code above you are retrieving a using GET.
PHP
$name =$_GET['Name'];
$id =$_GET['id'];
$Class =$_GET['Class'];
$Law =$_GET['Law'];
$Hoqoq =$_GET['Hoqoq'];
$PanelCode =$_GET['Law panel'];


Read about them: POST (works with headers) vs GET (it works with query strings - data after ? in url): PHP - GET & POST Methods - Tutorialspoint[^]

Assuming you made a right POST call, following is the way to retrieve data:
PHP
if(isset($_POST['save']))
{	 
    $id =$_POST['id'];
    $Class =$_POST['Class'];
    $Law =$_POST['Law'];
    $Hoqoq =$_POST['Hoqoq'];
    $PanelCode =$_POST['Law panel'];
    $sql =...
}

A sample for your reference.: Insert Data Into MySQL Using PHP[^]

As R.Deeming shared, make sure to have parameterized query to avoid SQL Injection.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900