1

I'm having a html form where i have 3 fields(ID, NAME, BLOOD GROUP). And below that I've 3 buttons(INSERT, UPDATE, DELETE). And then i want to enter the values in the fields and when click on the INSERT button these values should be entered into a table through a PHP script. Similar respective actions must happen with UPDATE and DELETE also. Can anyone help me out?

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if (!$conn) {
    die("couldn't connect" . mysql_error());
}
echo 'connected successfully';
mysql_select_db('DB');

$id = $_POST['Id'];
$name = $_POST['Name'];
$blood = $_POST['BloodGroup'];
$order = "Insert into info(Id, name, BloodGroup) values ('$Id', '$Name', '$BloodGroup')";

$result = mysql_query($order);

if (!$result) {
    die('Input data failed' . mysql_error());
}
echo 'Input data entered successfully';
mysql_close($conn);
6
  • Can you show us what have you tried? Commented Apr 10, 2015 at 6:48
  • <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn) { die("couldn't connect" . mysql_error()); } echo 'connected successfully'; mysql_select_db('DB'); $id = $_POST['Id']; $name = $_POST['Name']; $blood = $_POST['BloodGroup']; $order = "Insert into info(Id, name, BloodGroup) values ('$Id', '$Name', '$BloodGroup')"; $result = mysql_query($order); if(! $result) { die('Input data failed' . mysql_error()); } echo 'Input data entered successfully'; mysql_close($conn); ?> Commented Apr 10, 2015 at 6:53
  • This is what i've done for insert. I don't know if it's correct or not. Commented Apr 10, 2015 at 6:54
  • First of all I would advise to change the mysql_ functions to PDO or mysqli since your code is prone to mysql injection stackoverflow.com/questions/60174/… Commented Apr 10, 2015 at 7:00
  • 1
    Use mysql_select_db('DB') or die(mysql_error()); once Commented Apr 10, 2015 at 7:03

2 Answers 2

1

Firstly make sure your form is submitting to the PHP script.

For this the INSERT button should be of type="submit". Then you can debug your PHP script line by line. For UPDATE and DELETE you can use the same PHP script with the query strings. Ex - ?id=xx&action=update OR ?id=zz&action=delete. Then you can apply the code in your PHP script corresponding to the action.

Sign up to request clarification or add additional context in comments.

Comments

0

Use mysql_select_db('DB') or die(mysql_error()); once. and also your variable names are wrong.

Please Use

$order = "Insert into info(Id, name, BloodGroup) values ('$id', '$name', '$blood')";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.