0

I tested the variables in the update statement and checked if a database connection is established, however the query doesn't run, can you please show me the error in my code.

for($i=0; $i <= $numcourses; $i++){
    echo '<div class="new'.$i.'" id="new'.$i.'"><label>'.$course_names[$i].'</label>
    <input name="edit'.$i.'" type="submit" value="Edit" /><input name="delete'.$i.'" type="submit" value="Delete" /><br /></div>';
    $name="edit".$i;    
    if (isset($_POST[$name])){
        echo '<input name="text" type="text" value="'.$course_names[$i].'" /><input name="save'.$i.'" type="submit" value="Save"/>';
    }   
    $name2="save".$i;
    if (isset($_POST[$name2])){
        include "includes/open.php";
            $newname=($_POST['text']);
            $int=$i+1;
            $query = "UPDATE course SET cname = '".$newname."' WHERE cid = '".$int."'";
            mysql_query($query) or die(mysql_error());
            include "includes/close.php";
    }
}

Update: Thanx Marc B, adding or die(mysql_error());showed me the error in my code, everything works again and I'm back on track.

6
  • Have you tried just running the query in the database (sans PHP)? What error message do you get...? Commented May 3, 2012 at 14:41
  • If you need any more info to help me solve this problem, please let me know. Commented May 3, 2012 at 14:42
  • That's why I asked for the error message... Commented May 3, 2012 at 14:42
  • Ok... so you are trying to update a record, while using an incremented value. Are you sure that those 'cid'-s match. Commented May 3, 2012 at 14:46
  • As you're just starting out, forget all that stuff about mysql_query and so on. Move to MySQLi and prepared statements etc. The mysql_* stuff is all deprecated. (Or will be soon) Commented May 3, 2012 at 14:46

5 Answers 5

3

You have no error handling on your query calls:

mysql_query($query) or die(mysql_error());
                   ^^^^^^^^^^^^^^^^^^^^^^

which would tell you if there's any problems with the query execution. On a meta level, you're wide open to SQL injection attacks, so you'd better read up about that and fix the problem before you go any further with your code.

Sign up to request clarification or add additional context in comments.

2 Comments

Aaaah, ok, didn't know. I added your code and it throws "No database selected", so a connection was probably not established, thanx. Going to see if I can get it to work, will update.
That'd be a missing mysql_select_db() call, in that case. If you hadn't connected at all, it'd say "no connection" or something similar. Never assume a query call succeeded. Even if the SQL statement itself is 100% valid, there's way too many other reasons for things to blow up to NOT check for failure.
2
$query = "UPDATE course SET cname = '".$newname."' WHERE cid = '".$int."'";

is cID an integer ? in the update statement, looks to me like a string, try to echo every query and check the validity by executing it directly in your db

Comments

1

where do you connect to the database??

use mysql_connect(string hostname, string username, string password'); to connect to the database and then execute the query after selecting your database using mysql_select_db..

1 Comment

I establish the connection in includes, it was missing from my code which caused the error.
1

First you should remove the extra ; on $name="edit".$i;;

Then, how do you post the values? I see no <form> attributes in your code, hence it cannot be posted.

Also, everything is in a for loop. $newname=($_POST['text']); is never being set.

Comments

1

Maybe instead of this:

if (isset($_POST[$name2]))

try this:

if ($name2!="")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.