Jump to content

select then insert

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
techker

techker

    Programmer

  • Members
  • PipPipPipPip
  • 136 posts
hey guys i made a small account registration .in order to register you need a member id.

now on the add account page i need to check first if the id is used if not i register it..


//i have this but im wondering how to do the if user id already taken echo done or else register the account..


$q = "SELECT * FROM web_access WHERE client_id ='$client_id'";

$res = mysql_query($q);

$row = mysql_fetch_assoc($res);


//Writes the information to the database

$query =("INSERT INTO web_access (client_id,username,password,date,login_count) ".

        "VALUES ('$client_id','$username','$password','$date','$login_count')");

//Writes the photo to the server

$dcdc=mysql_query($query);


if (!$dcdc) {

    // Error

    echo "Sorry, there was an updating  your file $id.";

} else {

    //Writes the photo to the server

    

          echo "<meta http-equiv=Refresh content=2;url=main_clients.php>";

}



#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Hey Techker, what seems to be the problem then? You can count the results from $row or modify your SELECT statement and add COUNT():

$q = "SELECT COUNT(client_id) as cnt FROM web_access WHERE client_id ='$client_id'"; 
$res = mysql_query($q); 
$row = mysql_fetch_assoc($res); 

if ($row['cnt'] <= 0) {
//Writes the information to the database 
$query =("INSERT INTO web_access (client_id,username,password,date,login_count) ". 
        "VALUES ('$client_id','$username','$password','$date','$login_count')"); 
//Writes the photo to the server 
$dcdc=mysql_query($query); 

}

This will cause the insert statement to only execute when there are no values. Is this what you needed?

#3
techker

techker

    Programmer

  • Members
  • PipPipPipPip
  • 136 posts
ya something like that but not to count but to verify if the client id is already used

if not used it insert if used it gives an echo id used already..

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Counting will verify that it is used or not. If you select one row that has that client-id it means that it is already used. You can also make it a Unique Key or a Primary Key in your database and it will only be allowed one entry and will throw a DB error on insert otherwise.

#5
techker

techker

    Programmer

  • Members
  • PipPipPipPip
  • 136 posts
i forgot about that thx!will try it out.

#6
techker

techker

    Programmer

  • Members
  • PipPipPipPip
  • 136 posts
in your code can we add an error txt?

like sorry user id already used