Php delete from table sql not working

Hi I’m running XAMPP with the following
• Server: 127.0.0.1 via TCP/IP
• Software: MySQL
• Software version: 5.5.27 - MySQL Community Server (GPL)
• Protocol version: 10
• User: admin@localhost
• Server charset: UTF-8 Unicode (utf8)
• Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
• Database client version: libmysql - mysqlnd 5.0.10 - 20111026 - $Id: b0b3b15c693b7f6aeb3aa66b646fee339f175e39 $
• PHP extension: mysql

The html form take the users text input and activates the php script but it seems I have gone wrong some ware and I need help. The SQL database is called (test) table is called (teacher) rows in table are
Id and name.

THE HTML CODE

[code]

Delete Student Form

Delete a Teacher Record

Enter the Name of the Teacher to be deleted:

Name:

[/code]

The PHP CODE
[php]<?php

// Read name from form using $_POST (safest)

$id=$_POST[“name”];

// Connect to server
// Replace username and password by your details

$db = @mysql_connect(“localhost”,“username”,“password”);
if (!$db)
{
do_error(“Could not connect to the server”);
}

// Connect to the database
// Note that your database will be called username

@mysql_select_db(“test”,$db)or do_error(“Could not connect to the database”);

// Run query

$sql=“DELETE FROM teacher WHERE name=$id”;

$result=mysql_query($sql,$db);

// Check that query resulted in a deleted record

$affected=mysql_affected_rows();

if ($affected)
{

    // $affected is non-zero 

    echo "Record: $id has been deleted."; 

}
else
{

    // $affected is zero so record does not exist 

    do_error("No such record"); 

}

function do_error($error)
{
echo $error;
die;
}

?> [/php]

Thank you for any help give.

hi i think its a simple thing try changing this
[php]
$sql=“DELETE FROM teacher WHERE name=$id”;
[/php]
to this i think its actually looking for $id as a string not the variables value.
[php]
$sql=“DELETE FROM teacher WHERE name=’$id’”;
[/php]
hope this helps ;D

Thanks cogga28 that fixed it

your welcome bloody syntax is a pain sometimes and always takes a fresh set of eyes

Sorry to be a pain but this is why i simplified my code to try and fix my issue but im still getting a issue with the following

So expanding my code I am running into problems need help
Delete row from table SQL
Html from than uses php script called php.php to get names from SQL database and display them in drop down menu. This works…….

Php script called test2.php ths is the script to delete the drop down menu bit I keep getting an error

PHP.php
[php]<?php

// Connect to server
// Replace username and password by your details

$db = @mysql_connect(“localhost”,“my username”,“mypassword”);
if (!$db)
{
do_error(“Could not connect to the server”);
}

// Connect to the database
// Note that your database will be called username

@mysql_select_db(“test”,$db)or do_error(“Could not connect to the database”);

//connect to db first
$options = ‘’;
$teachers = mysql_query(‘SELECT * FROM teacher ORDER BY name ASC’);
while($teacher = mysql_fetch_array($teachers)) {
$options .= sprintf("%s", $teacher[‘name’], $teacher[‘name’]);
}
$class_options = ‘’;
$classes = mysql_query(‘SELECT * FROM class’);
while($class = mysql_fetch_array($classes)) {
$class_options .= sprintf("%s", $class[‘name’], $class[‘name’]);
}
$room_options = ‘’;
$rooms = mysql_query(‘SELECT * FROM room’);
while($room = mysql_fetch_array($rooms)) {
$room_options .= sprintf("%s", $room[‘number’], $room[‘number’]);
}
$subject_options = ‘’;
$subjects = mysql_query(‘SELECT * FROM subject’);
while($subject = mysql_fetch_array($subjects)) {
$subject_options .= sprintf("%s", $subject[‘name’], $subject[‘name’]);
}
?>[/php]

my HTML form…well not realy contains php
[php]

Delete Student Form

Delete a Teacher Record

Enter the Name of the Teacher to be deleted:

<?php include('php.php'); ?>

DEL Teacher: <?php echo $options ?>


[/php]

and my PHP scrippt to delete user name …test2.php
[php]<?php

// Read name from form using $_POST (safest)

$id=$_POST[“name”];

// Connect to server
// Replace username and password by your details

$db = @mysql_connect(“localhost”,“root”,“iqonr301”);
if (!$db)
{
do_error(“Could not connect to the server”);
}

// Connect to the database
// Note that your database will be called username

@mysql_select_db(“test”,$db)or do_error(“Could not connect to the database”);

// Run query

$sql=“DELETE FROM teacher WHERE name=’$id’”;

$result=mysql_query($sql,$db);

// Check that query resulted in a deleted record

$affected=mysql_affected_rows();

if ($affected)
{

    // $affected is non-zero 

    echo "Record: $id has been deleted."; 

}
else
{

    // $affected is zero so record does not exist 

    do_error("No such record"); 

}

function do_error($error)
{
echo $error;
die;
}

?> [/php]

error i am getting is
Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 26
No such record

so line 26 is back on the
$sql=“DELETE FROM teacher WHERE name=’$id’”;

thank you for you help

[php]
$sql=“DELETE FROM teacher WHERE name=’$id’”
}
[/php]
both of these below work for me
[php]
//Create delete query
$qry = “DELETE FROM teacher WHERE name=’$id’”;
$result = @mysql_query($qry);

//Check whether the query was successful or not
if($result) {

    echo "Record: $id has been deleted."; 

    }

[/php]

[php]
//Create delete query
$qry = “DELETE FROM teacher WHERE name=$id”;
$result = @mysql_query($qry);

//Check whether the query was successful or not
if($result) {

    echo "Record: $id has been deleted."; 

    }

[/php]

Hi i am getting a syntax error, unexpected ON LINE 28 and i think 33 could some one have a look thanks

[php]<?php

// Read name from form using $_POST (safest)

$id=$_POST[“name”];

// Connect to server
// Replace username and password by your details

$db = @mysql_connect(“localhost”,“root”,“808080808”);
if (!$db)
{
do_error(“Could not connect to the server”);
}

// Connect to the database
// Note that your database will be called username

@mysql_select_db(“test”,$db)or do_error(“Could not connect to the database”);

//Create delete query
$qry = “DELETE FROM teacher WHERE name=’$id’”;
$result = @mysql_query($qry);
//Check whether the query was successful or not if
($result){
echo “Record: $id has been deleted.”;
{

}
else
{
// $affected is zero so record does not exist

do_error(“No such record”);

}

function do_error($error)
{
echo $error;
die;
}

?>[/php]

it was this line i had [php]//Check whether the query was successful or notif
($result) { [/php]
should have had [php]//Check whether the query was successful or not
if
($result) { [/php]

Hi i get a error on my script so i am going to add all myy scripts here to see if some one can help.

HTLM form page looks to the php.php script to get the name of teachers from a table and dspaly them in a drop down menu.
[php]

Delete Student Form

Delete a Teacher Record

Enter the Name of the Teacher to be deleted:

<?php include('php.php'); ?>

DEL Teacher: <?php echo $options ?>


[/php]

This is the php.php script tht get the names from the database table
[php]<?php

// Connect to server
// Replace username and password by your details

$db = @mysql_connect(“localhost”,“root”,“iqonr301”);
if (!$db)
{
do_error(“Could not connect to the server”);
}

// Connect to the database
// Note that your database will be called username

@mysql_select_db(“test”,$db)or do_error(“Could not connect to the database”);

//connect to db first
$options = ‘’;
$teachers = mysql_query(‘SELECT * FROM teacher ORDER BY name ASC’);
while($teacher = mysql_fetch_array($teachers)) {
$options .= sprintf("%s", $teacher[‘name’], $teacher[‘name’]);
}
$class_options = ‘’;
$classes = mysql_query(‘SELECT * FROM class’);
while($class = mysql_fetch_array($classes)) {
$class_options .= sprintf("%s", $class[‘name’], $class[‘name’]);
}
$room_options = ‘’;
$rooms = mysql_query(‘SELECT * FROM room’);
while($room = mysql_fetch_array($rooms)) {
$room_options .= sprintf("%s", $room[‘number’], $room[‘number’]);
}
$subject_options = ‘’;
$subjects = mysql_query(‘SELECT * FROM subject’);
while($subject = mysql_fetch_array($subjects)) {
$subject_options .= sprintf("%s", $subject[‘name’], $subject[‘name’]);
}
?>[/php]

i think my proble is with my html form and my php delete script
i think that my html form is not saying what name i am selecting from the database and passing it on the the delete php script so once i run this script i get the frollowing
Notice: Array to string conversion in C:\xampp\htdocs\test2.php on line 22
No such record

[php]<?php

// Read name from form using $_POST (safest)

$id=$_POST[“name”];

// Connect to server
// Replace username and password by your details

$db = @mysql_connect(“localhost”,“root”,“iqonr301”);
if (!$db)
{
do_error(“Could not connect to the server”);
}

// Connect to the database
// Note that your database will be called username

@mysql_select_db(“test”,$db)or do_error(“Could not connect to the database”);

//Create delete query
$qry = “DELETE FROM teacher WHERE name=$id”;
$result = @mysql_query($qry);
//Check whether the query was successful or not
if($result)
{
echo “Record: $id has been deleted.”;
}
{
// $affected is zero so record does not exist

do_error(“No such record”);

}

function do_error($error)
{
echo $error;
die;
}

?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service