please help y is this code not updating my database

Add New Record in MySQL Database <?php

$mysql_host = ‘localhost’;
$mysql_user = ‘root’;
$mysql_pass = ‘’;

//database
$mysql_db=‘safabc’;

//Database Connection
$conn = mysqli_connect($mysql_host, $mysql_user, $mysql_pass);

//errors
$conn_error = ‘Wrong user name or password’;
$dbcon_error = ‘Fail to connect to Database’;

if(! $conn )

{
die (‘Could not connect:’ . $dbcon_error());
}

@mysql_select_db(“root”, $conn);

$sql = “INSERT INTO players ,
(Reg_ID, Card_No, ID_Number, Player_Name, Player_Surname, Club, LFA, League, Date_Registered)
VALUES
(NULL,’$_POST[Card_No]’, ‘$_POST[ID_Number]’, ‘$_POST[Player_Name]’, ‘$_POST[Player_Surname]’, ‘$_POST[Club]’, ‘$_POST[LFA]’, ‘$_POST[League]’, Date_Registered)”;

if (!@mysql_query($sql,$conn))
{
die('Error: ’ . mysql_error());
}
echo “1 record added”;

mysql_close($conn)

?>

You are mixing two types of database interaction.

Here, you setup a connection using mysqli

Then here you are trying to do a query using the old mysql

You can’t use both like that.
Use mysqli only as mysql was deprecated years ago and is extremely unsafe.

And one last thing, stop using the @ symbol!
This is an error suppressor - hence why you can’t see what’s wrong in your code!

Hope that helps,
Red :wink:

PS: Just noticed something else too.
In your query where you are using $_POST variables they need to be wrapped in curly braces {} like so;
[php]’{$_POST[Card_No]}’[/php]
Whilst we’re on the subject, it’s extremely bad practice to allow input from a user to be placed directly into the database without first sanitizing the data for any malicious code.

Another thing, remove the Reg_ID and null value from your query.

Error Codes when running the script

( ! ) Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\wamp\www\safabc\register_players.php on line 29
Call Stack

Time Memory Function Location

1 0.0010 140640 {main}( ) …\register_players.php:0
2 0.0160 148480 mysqli_select_db ( ) …\register_players.php:29

( ! ) Notice: Use of undefined constant Card_No - assumed ‘Card_No’ in C:\wamp\www\safabc\register_players.php on line 34
Call Stack

Time Memory Function Location

1 0.0010 140640 {main}( ) …\register_players.php:0

( ! ) Notice: Use of undefined constant ID_Number - assumed ‘ID_Number’ in C:\wamp\www\safabc\register_players.php on line 34
Call Stack

Time Memory Function Location

1 0.0010 140640 {main}( ) …\register_players.php:0

( ! ) Notice: Use of undefined constant Player_Name - assumed ‘Player_Name’ in C:\wamp\www\safabc\register_players.php on line 34
Call Stack

Time Memory Function Location

1 0.0010 140640 {main}( ) …\register_players.php:0

( ! ) Notice: Use of undefined constant Player_Surname - assumed ‘Player_Surname’ in C:\wamp\www\safabc\register_players.php on line 34
Call Stack

Time Memory Function Location

1 0.0010 140640 {main}( ) …\register_players.php:0

( ! ) Notice: Use of undefined constant Club - assumed ‘Club’ in C:\wamp\www\safabc\register_players.php on line 34
Call Stack

Time Memory Function Location

1 0.0010 140640 {main}( ) …\register_players.php:0

( ! ) Notice: Use of undefined constant LFA - assumed ‘LFA’ in C:\wamp\www\safabc\register_players.php on line 34
Call Stack

Time Memory Function Location

1 0.0010 140640 {main}( ) …\register_players.php:0

( ! ) Notice: Use of undefined constant League - assumed ‘League’ in C:\wamp\www\safabc\register_players.php on line 34
Call Stack

Time Memory Function Location

1 0.0010 140640 {main}( ) …\register_players.php:0

( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\safabc\register_players.php on line 37
Call Stack

Time Memory Function Location

1 0.0010 140640 {main}( ) …\register_players.php:0
2 0.0380 149144 mysqli_query ( ) …\register_players.php:37

( ! ) Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\wamp\www\safabc\register_players.php on line 39
Call Stack

Time Memory Function Location

1 0.0010 140640 {main}( ) …\register_players.php:0
2 0.0410 149152 mysqli_error ( ) …\register_players.php:39
Error:

Updated Code

Add New Record in MySQL Database <?php

$mysqli_host = ‘localhost’;
$mysqli_user = ‘root’;
$mysqli_pass = ‘’;

//database
$mysqli_db=‘safabc’;

//Database Connection
$conn = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_pass);

//errors
$conn_error = ‘Wrong user name or password’;
$dbcon_error = ‘Fail to connect to Database’;

if(! $conn )

{
die (‘Could not connect:’ . $dbcon_error());
}

mysqli_select_db(‘root’, $conn);

$sql = “INSERT INTO players ,
(Card_No, ID_Number, Player_Name, Player_Surname, Club, LFA, League, Date_Registered)
VALUES
(’{$_POST[Card_No]}’, ‘{$_POST[ID_Number]}’, ‘{$_POST[Player_Name]}’, ‘{$_POST[Player_Surname]}’, ‘{$_POST[Club]}’, ‘{$_POST[LFA]}’, ‘{$_POST[League]}’, Date_Registered)”;

if (!mysqli_query($sql,$conn))
{
die('Error: ’ . mysqli_error());
}
echo “1 record added”;

mysqli_close($conn)

?>

Your missing the single quotes on all your variable like here:

{$_POST[[size=14pt][/size]ID_Number[size=14pt][/size]]}

[size=18pt]Now im getting these error codes[/size]

( ! ) Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\wamp\www\safabc\register_players.php on line 29
Call Stack

Time Memory Function Location

1 0.0500 139768 {main}( ) …\register_players.php:0
2 0.0840 147592 mysqli_select_db ( ) …\register_players.php:29

( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\safabc\register_players.php on line 37
Call Stack

Time Memory Function Location

1 0.0500 139768 {main}( ) …\register_players.php:0
2 0.0900 148176 mysqli_query ( ) …\register_players.php:37

( ! ) Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\wamp\www\safabc\register_players.php on line 39
Call Stack

Time Memory Function Location

1 0.0500 139768 {main}( ) …\register_players.php:0
2 0.0960 148168 mysqli_error ( ) …\register_players.php:39
Error:

[size=18pt]php Code[/size]

[php]

Add New Record in MySQL Database <?php

$mysqli_host = ‘localhost’;
$mysqli_user = ‘root’;
$mysqli_pass = ‘’;

//database
$mysqli_db=‘safabc’;

//Database Connection
$conn = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_pass);

//errors
$conn_error = ‘Wrong user name or password’;
$dbcon_error = ‘Fail to connect to Database’;

if(! $conn )

{
die (‘Could not connect:’ . $dbcon_error());
}

mysqli_select_db(‘root’, $conn);

$sql = “INSERT INTO players ,
(Card_No, ID_Number, Player_Name, Player_Surname, Club, LFA, League, Date_Registered)
VALUES
(’{$_POST[‘Card_No’]}’, ‘{$_POST[‘ID_Number’]}’, ‘{$_POST[‘Player_Name’]}’, ‘{$_POST[‘Player_Surname’]}’, ‘{$_POST[‘Club’]}’, ‘{$_POST[‘LFA’]}’, ‘{$_POST[‘League’]}’, Date_Registered)”;

if (!mysqli_query($sql,$conn))
{
die('Error: ’ . mysqli_error());
}
echo “1 record added”;

mysqli_close($conn)

?>

[/php]

Please use the forum PHP code tags. (I updated your post.)

Did you really name your database “root”? You have done nothing with this:

//database
$mysqli_db=‘safabc’;

IM still getting errors
My Database name is safabc and i have corrected line 29 accordingly

( ! ) Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\wamp\www\safabc\register_players.php on line 29
Call Stack

Time Memory Function Location

1 0.0030 139832 {main}( ) …\register_players.php:0
2 0.0720 147656 mysqli_select_db ( ) …\register_players.php:29

( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\safabc\register_players.php on line 37
Call Stack

Time Memory Function Location

1 0.0030 139832 {main}( ) …\register_players.php:0
2 0.0770 148256 mysqli_query ( ) …\register_players.php:37

( ! ) Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\wamp\www\safabc\register_players.php on line 39
Call Stack

Time Memory Function Location

1 0.0030 139832 {main}( ) …\register_players.php:0
2 0.0850 148248 mysqli_error ( ) …\register_players.php:39
Error:

[size=14pt]Here is my form code[/size]

[php]

Add New Record in MySQL Database
Card No
ID Number
Player Name
Player Surname
Club <?php @mysql_connect('localhost', 'root', ''); //mysql_select_db('safabc');

$sql = ‘SELECT Club FROM safabc.clubs’;
$result = mysql_query(‘SELECT Club FROM safabc.clubs’);
echo “<select name=“Club”>”;
echo “”;

while ($row = mysql_fetch_array($result))
 {
 echo "<option value='" . $row['Club'] . "'>" . $row['Club'] . "</option>";
}
echo "</select>"

?>
LFA Select LFA Bunkers West East London Central East London North East London West King Central LFA King East LFA MDALFA Royal Rarabe LFA
League
[/php]

[size=14pt][i][b]DataBase: Safabc

Table players[/b][/i][/size]

Column Type Null
Reg_ID int(11) No
ID_Number char(13) No
Player_Name varchar(30) No
Player_Surname varchar(30) No
Club varchar(30) No
League varchar(30) No
Date_Registered datetime No

Indexes

Keyname Type Unique Packed Column Cardinality Collation Null Comment
PRIMARY BTREE Yes No Reg_ID 0 A No
ID_Number BTREE Yes No ID_Number 0 A No

Why would you change everything to obsolete Mysql code?

Well, I was bored and I wanted to see if I could help you out, plus show you that’s not to hard to switch over to mysqli or PDO (This is what I used, I like PDO way better).

I modified the database structure a little bit:
CREATE TABLE IF NOT EXISTS players (
reg_id int(11) NOT NULL AUTO_INCREMENT,
id_number char(13) NOT NULL,
player_name varchar(30) NOT NULL,
player_surname varchar(30) NOT NULL,
club varchar(30) NOT NULL,
league varchar(30) NOT NULL,
date_registered timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’,
PRIMARY KEY (reg_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

And here is the code that I did:
[php]<?php
try {
$db_options = array(
/* important! use actual prepared statements (default: emulate prepared statements) /
/
throw exceptions on errors (default: stay silent) /
/
fetch associative arrays (default: mixed arrays) */
PDO::ATTR_EMULATE_PREPARES => false
, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);

$pdo = new PDO('mysql:host=localhost;dbname=safabc;charset=utf8', 'root (username usually root)', 'your_password_goes_here', $db_options);	

} catch (PDOException $e) { // Report the Error!
/* You never want to use the following on a production website */
$status_message = “

Something is not right, check your php.ini settings or code

”;
}

$data = array();

$submit = filter_input(INPUT_POST, ‘submit’, FILTER_SANITIZE_SPECIAL_CHARS);

if ( isset($submit) && $submit == ‘Submit’) {
$data[‘id_number’] = filter_input(INPUT_POST, ‘id_number’, FILTER_SANITIZE_SPECIAL_CHARS);
$data[‘player_name’] = filter_input(INPUT_POST, ‘player_name’, FILTER_SANITIZE_SPECIAL_CHARS);
$data[‘player_surname’] = filter_input(INPUT_POST, ‘player_surname’, FILTER_SANITIZE_SPECIAL_CHARS);
$data[‘club’] = filter_input(INPUT_POST, ‘club’, FILTER_SANITIZE_SPECIAL_CHARS);
$data[‘league’] = filter_input(INPUT_POST, ‘league’, FILTER_SANITIZE_SPECIAL_CHARS);

/* Setup the query for the database table */
$query = 'INSERT INTO players 
(id_number, player_name, player_surname, club, league, date_registered)
VALUES 
(:id_number, :player_name, :player_surname, :club, :league, NOW())
';	

/* Prepare the query */
$stmt = $pdo->prepare($query);

/* Execute the Query */
$result = $stmt->execute( array(
	':id_number' => $data['id_number'],
	':player_name' => $data['player_name'],
	':player_surname' => $data['player_surname'],
	':club' => $data['club'],
	':league' => $data['league']
	));

/* Display the result of the insertion */
if ($result) {
	$status_message = "Data Successfully inserted";
} else {
	$status_message = "Insertion Failed";
}

}

?>
<!doctype html>

Database Table Insert

Status of Insertion: <?php echo (isset($status_message)) ? $status_message : 'Waiting...'; ?>

First Name
Last Name

Detroit Tigers Kansas City Royals Cleveland Indians Chicago White Sox Minnesota Twins

American League National League

[/php]

I also took used American Baseball as testing the data, but that can be easily converted over to soccer (err I mean football)? ;D If nothing else this shows you that it’s not to hard to switch over to mysqli or PDO, using php.net as a way to help you understand mysqli or PDO is a great way to start. FYI - There is no data validation being done.

Thank you very much for your assistance im still new
, now i have set ID number to be unique, how do I make it to return a duplicate entry warning and echo the duplicating value

You need to create a Unique index on that field in the database and then catch the error from the database on a duplicate attempt.

Sponsor our Newsletter | Privacy Policy | Terms of Service