1

I want to alert the user if the query or updating of the database is successful or not..

adduser.php

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

$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$middlename = $_POST['mname'];
$password = $_POST['pword'];
$username = $_POST['uname'];
$gender = $_POST['gender'];
$utype = $_POST['utype'];

$query = "INSERT INTO user (firstname,lastname,middlename,gender) VALUES ('$firstname','$lastname','$middlename','$gender')";   

mysql_query($query);

$result = mysql_query("SELECT id FROM user WHERE firstname = '$firstname'");

$row = mysql_fetch_assoc($result);

mysql_query("INSERT INTO accounts (u_id,username,password,utype) VALUES ('$row["id"]','$username',md5('$password'),$utype);")
?>

how would I alert the user?or what should I use to give the user a pop-up message saying its successful?

Niket Malik
  • 1,075
  • 1
  • 14
  • 23
jmjassy27
  • 77
  • 1
  • 5
  • 10
  • 1
    Just a sidenote, your code is vulnerable to SQL injections, and `mysql_*` functions are deprecated. You may wish to read [How can I prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) and [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). –  Aug 22 '13 at 02:21
  • @user2062950 thanks..is it better to use mysqli_? – jmjassy27 Aug 22 '13 at 02:24
  • 1
    Yes, but PDO works too. There's an [Overview of the MySQL PHP drivers](http://www.php.net/manual/en/mysql.php) that provides good info on them. –  Aug 22 '13 at 02:28
  • 1
    @jmjassy27 The important part is that you use prepared/parameterized queries. You need to fundamentally separate the data from the command to avoid SQL injection attacks. – Brad Aug 22 '13 at 02:43

3 Answers3

1

Check number of affected rows, reference http://php.net/manual/en/function.mysql-affected-rows.php.

Also mysql_* functions are depreciated, try using mysqli_* or PDO.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
Niket Malik
  • 1,075
  • 1
  • 14
  • 23
0

Assign a variable to mysql_query, for example:

 $result = mysql_query("your query");

 if($result){
 $xxx = "query successful";
 }else{
 $xxx = "query failed";
 }

In the hmtl:

 <script>
 alert(<?php echo $xxx;?>);
 </script>

This is one way, from here you can play to do whatever you want


As side Note: Mysql_* extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

A useful link Why shouldn't I use mysql_* functions in PHP

Community
  • 1
  • 1
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
0

mysql_query() returns false for invalid query. mysql_num_rows() returns number of records returned for query like select. mysql_affected_rows() checks for affected rows.

EG:

$result = mysql_query("your query");

if($result){ 
  //query successful but this doesn't mean it returned anything
  //you can now use mysql_num_rows (for select query) to check if something is returned
  if( mysql_num_rows($result) > 0 ){
   //some results were returned
  }

  //for insert, update or delete query you need to check affected rows like this
  if( mysql_affected_rows() > 0 ){
   //some records were updated or deleted
  }
}else {
  echo "There was a problem";
}

Sidenote: MySQL_* extension is deprecated as of PHP 5.5.0, and will be removed in the future. Use MySQLi or PDO instead.

Konsole
  • 3,447
  • 3
  • 29
  • 39