Jump to content

Recommended Posts

Because the syntax for a delete query is (bold part is the most common usage) -

 

DELETE [LOW_PRIORITY] [QUICK] [iGNORE] FROM tbl_name

    [WHERE where_condition]

    [ORDER BY ...]

    [LIMIT row_count]

 

A typical query would look like -

 

DELETE FROM your_table WHERE some_id_column = some_id_value

 

Instead of making another topic, I'll post here.

 

How come it doesn't give me the error that a clan with the same name already exists?

 

   if ($check!=1) 
	 {
  mysql_query("INSERT INTO clans VALUES ('', '$clan_name', '$session', '$ip', '', '$date')");
    $extract = mysql_query("SELECT clan_id FROM clans WHERE clan_name='$clan_name'");
	while ($row = mysql_fetch_assoc($extract)) 
	   {  $clan_id = $row['clan_id'];  } 
	   
	     mysql_query("UPDATE users SET clan='$clan_id' WHERE username='$session'");

  echo "The clan ". $clan_name ." has been registered successfully, as ". $session ." (you) being the leader. <a href='index.php'>Home</a>";
   } else {
   echo "A clan with this name already exists! <a href='register_clan.php'>Back</a>";
 }
}

I don't see your conditional at all?

 

Instead of making another topic, I'll post here.

 

How come it doesn't give me the error that a clan with the same name already exists?

 

   if ($check!=1) 
	 {
  mysql_query("INSERT INTO clans VALUES ('', '$clan_name', '$session', '$ip', '', '$date')");
    $extract = mysql_query("SELECT clan_id FROM clans WHERE clan_name='$clan_name'");
	while ($row = mysql_fetch_assoc($extract)) 
	   {  $clan_id = $row['clan_id'];  } 
	   
	     mysql_query("UPDATE users SET clan='$clan_id' WHERE username='$session'");

  echo "The clan ". $clan_name ." has been registered successfully, as ". $session ." (you) being the leader. <a href='index.php'>Home</a>";
   } else {
   echo "A clan with this name already exists! <a href='register_clan.php'>Back</a>";
 }
}

I don't see your conditional at all?

 

Instead of making another topic, I'll post here.

 

How come it doesn't give me the error that a clan with the same name already exists?

 

   if ($check!=1) 
	 {
  mysql_query("INSERT INTO clans VALUES ('', '$clan_name', '$session', '$ip', '', '$date')");
    $extract = mysql_query("SELECT clan_id FROM clans WHERE clan_name='$clan_name'");
	while ($row = mysql_fetch_assoc($extract)) 
	   {  $clan_id = $row['clan_id'];  } 
	   
	     mysql_query("UPDATE users SET clan='$clan_id' WHERE username='$session'");

  echo "The clan ". $clan_name ." has been registered successfully, as ". $session ." (you) being the leader. <a href='index.php'>Home</a>";
   } else {
   echo "A clan with this name already exists! <a href='register_clan.php'>Back</a>";
 }
}

$get_other = mysql_query("SELECT FROM clans WHERE clan_name='$clan_name'");
 $check = mysql_num_rows($get_other);
    if ($check!=1) 
	 {
  mysql_query("INSERT INTO clans VALUES ('', '$clan_name', '$session', '$ip', '', '$date')");
    $extract = mysql_query("SELECT clan_id FROM clans WHERE clan_name='$clan_name'");
	while ($row = mysql_fetch_assoc($extract)) 
	   {  $clan_id = $row['clan_id'];  } 
	   
	     mysql_query("UPDATE users SET clan='$clan_id' WHERE username='$session'");

  echo "The clan ". $clan_name ." has been registered successfully, as ". $session ." (you) being the leader. <a href='index.php'>Home</a>";
   } else {
   echo "A clan with this name already exists! <a href='register_clan.php'>Back</a>";
 }
}

         $get_other = mysql_query("SELECT FROM clans WHERE clan_name='$clan_name'");
 $check = mysql_num_rows($get_other);

         if ($check!=1) 
	 {
          mysql_query("INSERT INTO clans VALUES ('', '$clan_name', '$session', '$ip', '', '$date')");
          $extract = mysql_query("SELECT clan_id FROM clans WHERE clan_name='$clan_name'");
	  while ($row = mysql_fetch_assoc($extract)) 
	   {  
	   $clan_id = $row['clan_id'];  
	   } 
	   
	   mysql_query("UPDATE users SET clan='$clan_id' WHERE username='$session'");
           echo "The clan ". $clan_name ." has been registered successfully, as ". $session ." (you) being the leader. <a href='index.php'>Home</a>";

             } else {

   echo "A clan with this name already exists! <a href='register_clan.php'>Back</a>";
 }
}

I'll assume $clan_name, $session, $ip and $date have already been validated and sanitized.

<?php
$sql = "SELECT FROM clans WHERE clan_name='$clan_name'";
if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
        echo "A clan with this name already exists! <a href='register_clan.php'>Back</a>";
    } else {
        $sql = "INSERT INTO clans VALUES ('', '$clan_name', '$session', '$ip', '', '$date')";
        if (mysql_query($sql)) {
            if (mysql_affected_rows()) {
                $clan_id = mysql_insert_id();
                $sql = "UPDATE users SET clan='$clan_id' WHERE username='$session'";
                if (mysql_query($sql)) {
                    echo "The clan ". $clan_name ." has been registered successfully, as ". $session ." (you) being the leader. <a href='index.php'>Home</a>";
                } else {
                    trigger_error(mysql_error() . ' ' . $sql);
                }
            }
        } else {
            trigger_error(mysql_error() . ' ' . $sql);
        }
    }
} else {
    trigger_error(mysql_error() . ' ' . $sql);
}
?>

I'll assume $clan_name, $session, $ip and $date have already been validated and sanitized.

<?php
$sql = "SELECT FROM clans WHERE clan_name='$clan_name'";
if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
        echo "A clan with this name already exists! <a href='register_clan.php'>Back</a>";
    } else {
        $sql = "INSERT INTO clans VALUES ('', '$clan_name', '$session', '$ip', '', '$date')";
        if (mysql_query($sql)) {
            if (mysql_affected_rows()) {
                $clan_id = mysql_insert_id();
                $sql = "UPDATE users SET clan='$clan_id' WHERE username='$session'";
                if (mysql_query($sql)) {
                    echo "The clan ". $clan_name ." has been registered successfully, as ". $session ." (you) being the leader. <a href='index.php'>Home</a>";
                } else {
                    trigger_error(mysql_error() . ' ' . $sql);
                }
            }
        } else {
            trigger_error(mysql_error() . ' ' . $sql);
        }
    }
} else {
    trigger_error(mysql_error() . ' ' . $sql);
}
?>

 

Not sure if I can use that code.....I don't really understand some of the stuff you put in there, and it's all different. What exactly did you add?

 

Edit:

 

if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
        echo "A clan with this name already exists! <a href='register_clan.php'>Back</a>";

 

What exactly does that check? And why didn't my code work? It's worked before. Like on my login page, I have no problems with it:

 

		   $check = mysql_query("SELECT username FROM users WHERE username='$username'");
			   $exist = mysql_num_rows($check);

			 if ($exist!=1) 
			   {
			     echo "No account exists with this username. <a href='login.php'>Back</a>";
			   }

 

Sort of new, so I'm still learning features.

What exactly does that check? And why didn't my code work?

 

The first if statement simply checks that the query worked. You should always check a query actually succeeds before trying to use any result from it.

 

The second if statement checks to see if the query returned any results, if it did it displays a message saying the clan already exists.

 

I didn't really read your code too much because its pretty messy, but you will notice there is a fair amount of error trapping in my code, this is what makes finding errors easier.

What exactly does that check? And why didn't my code work?

 

The first if statement simply checks that the query worked. You should always check a query actually succeeds before trying to use any result from it.

 

The second if statement checks to see if the query returned any results, if it did it displays a message saying the clan already exists.

 

I didn't really read your code too much because its pretty messy, but you will notice there is a fair amount of error trapping in my code, this is what makes finding errors easier.

 

Thank you for that post. I hope I'm not annoying this forum with all my topics and problems, I just want to learn as much as I can. And speaking of learning new functions/features, can you show me a simple example of the error trapping? Thats also new to me. :)

 

And with this:

 

if ($result = mysql_query($sql)) {

 

Where is $result set?

 

Also, tried doing what you did.....still doesn't work :(

 

   $check = mysql_query("SELECT FROM clans WHERE clan_id='$clan'");
   $numrows = mysql_num_rows($check);


   if($result = mysql_query($check)) {
      if(mysql_num_rows($result))
               { echo "Sorry, that clan doesn't exist! <a href='index.php'>Go home</a>"; } else {		  
        
		$extract = mysql_query("SELECT FROM clans WHERE clan_id='$clan'");
		while ($row = mysql_fetch_assoc($extract)) 
		 {
		    $clan_name = $row['clan_name'];
	            $clan_leader = $row['clan_leader'];
		 }

		 echo "Members:<br/>";

		 //to get memebers
		 $extract_members = mysql_query("SELECT FROM users WHERE clan='$clan'");
		 while ($row_get = mysql_fetch_assoc($extract_members))

		 {
		    echo "".$row['username']."";
	         }

	     }
	}
          }

Have another look over my code.

 

I did, everything matches up.

 

        if($result = mysql_query($check)) {
		    if(mysql_num_rows($result))
               { echo "Sorry, that clan doesn't exist! <a href='index.php'>Go home</a>"; } else {

It does NOT match up. There is no way I would format my code in such a poor manner for one. I am trying to show you best practices.

 

In reply #10 $check is the result of a call to mysql_query(), you then pass it again to mysql_query(). Programing is all about the details, give a computer one incorrect instruction and you will not be forgiven.

 

As for the rest of reply #10. The code is all over the place. Your using loops when you don't likely need them,  you have removed all error handling, variables seem to appear from nowhere and the list goes on.

 

I don't want to sound harsh but you need to look at the examples given.

All your telling me is how crappy my code is. Like I said, I'm new to PHP, sort of. I've been with it for 1 year now, and I'm still learning. Can you please tell me where all the "Crappy" or non-needed bits of code are? Like, what lines? Thanks.

 

And I get

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\project11\register_clan.php on line 25

 

for

 

<?php
session_start();
$session = $_SESSION['user'];

$mysql_host = "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_database = "project11";

mysql_connect($mysql_host, $mysql_username, $mysql_password);
mysql_select_db($mysql_database);

     if(!$session) {
    echo "Please <a href='login.php'>login</a> first, or <a href='index.php'>go home</a>.";
 } else {
  $clan_name = $_POST['clan'];
  $ip = $_SERVER['REMOTE_ADDR'];
  $date = date("M-D-Y");
  
 if (!$clan_name) {
  echo "Enter in a clan name: <form action='register_clan.php' method='POST'><input type='text' name='clan'><input type='submit' value='Register Clan'></form>";
 } else {

 $get_other = mysql_query("SELECT FROM clans WHERE clan_name='$clan_name'");
 $check = mysql_num_rows($get_other);

        if($result = mysql_query($get_other)) {
	  if(mysql_num_rows($result)) { 
	    echo "A clan with this name already exists."; } else {
  
  mysql_query("INSERT INTO clans VALUES ('', '$clan_name', '$session', '$ip', '', '$date')");
    $extract = mysql_query("SELECT clan_id FROM clans WHERE clan_name='$clan_name'");
	while ($row = mysql_fetch_assoc($extract)) 
	   {  $clan_id = $row['clan_id'];  } 
	   
	     mysql_query("UPDATE users SET clan='$clan_id' WHERE username='$session'");

  echo "The clan ". $clan_name ." has been registered successfully, as ". $session ." (you) being the leader. <a href='index.php'>Home</a>";
   }
   }
   }
   }

?>

Again same issue. In this example, $get_other is assigned the value returned by mysql_query(). You then, pass it to mysql_query().

 

I know your getting mad/upset with me...but.....you don't have to do this.

 

Your like talking to a wall. I don't know much, and I've used this type of code so many times...and it just stops on me. My login system and register system even run off of it, to see if an account exists or not. It's really p%^^ing me off now. :/

 

Edit:

 

Here is part of my login, and this code works:

 

		   $check = mysql_query("SELECT username FROM users WHERE username='$username'");
			   $exist = mysql_num_rows($check);

			 if ($exist!=1) 
			   {
			     echo "No account exists with this username. <a href='login.php'>Back</a>";
			   }

Oh, and the error you are getting in this example is because your query failed (and you didn't check it for success like I said (in my very post) that you always should.eg;

 

$get_other = mysql_query("SELECT FROM clans WHERE clan_name='$clan_name'");
$check = mysql_num_rows($get_other);

 

Should be....

 

if ($get_other = mysql_query("SELECT somefield FROM clans WHERE clan_name='$clan_name'")) {
  $check = mysql_num_rows($get_other);
} else {
  // handle error
}

 

Notice I also fixed your query to select somefield? This has all been posted before, you need to read the replies.

Your like talking to a wall. I don't know much, and I've used this type of code so many times...and it just stops on me. My login system and register system even run off of it, to see if an account exists or not. It's really p%^^ing me off now. :/

 

Just because your code has worked previously doesn't mean it was at all well written. I'm not getting mad or whatever, just trying to explain the benefits of writing good code. Do you want to write good code?

Oh, and the error you are getting in this example is because your query failed (and you didn't check it for success like I said (in my very post) that you always should.eg;

 

$get_other = mysql_query("SELECT FROM clans WHERE clan_name='$clan_name'");
$check = mysql_num_rows($get_other);

 

Should be....

 

if ($get_other = mysql_query("SELECT somefield FROM clans WHERE clan_name='$clan_name'")) {
  $check = mysql_num_rows($get_other);
} else {
  // handle error
}

 

Notice I also fixed your query to select somefield? This has all been posted before, you need to read the replies.

 

I do....it's just I need to learn the code before I understand it. And I know what your trying to do with somefield, but whats really the point in it? I'm just getting the num_rows. And I'll try it. I just don't get the if($get_other = part.

Your like talking to a wall. I don't know much, and I've used this type of code so many times...and it just stops on me. My login system and register system even run off of it, to see if an account exists or not. It's really p%^^ing me off now. :/

 

Just because your code has worked previously doesn't mean it was at all well written. I'm not getting mad or whatever, just trying to explain the benefits of writing good code. Do you want to write good code?

 

Haha yes, I do want to write good code.

 

Edit:

 

I made the error handling, simple but effective I guess

 

    if ($get_other = mysql_query("SELECT FROM clans WHERE clan_name='$clan_name'")) {
          $check = mysql_num_rows($get_other);
          } else {
          // handle error
	   echo "An error has occured.";
          }

 

It does echo out "An error has occured", so somethings wrong obviously.

As I said earlier, the if statement checks to see if the query fails or succeeds. It might be confusing because it also assigns a value to $get_other at the same time.

 

The same thing could be written as....

 

$get_other = mysql_query('SELECT foo FROM bar');
if ($get_other) {
  $check = mysql_num_rows($get_other);
} else {
  // query failed
}

 

You need to ALWAYS check the value returned by mysql_query isn't false before attempting to use it.

As I said earlier, the if statement checks to see if the query fails or succeeds. It might be confusing because it also assigns a value to $get_other at the same time.

 

The same thing could be written as....

 

$get_other = mysql_query('SELECT foo FROM bar');
if ($get_other) {
  $check = mysql_num_rows($get_other);
} else {
  // query failed
}

 

You need to ALWAYS check the value returned by mysql_query isn't false before attempting to use it.

 

Oh, alright. Makes sense now. And I edited my post above. So my query is failing.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.