Jump to content

Recommended Posts

Hello all. I'm tryign to update some records (for the first time) with;

//update DB 
    $sql = mysql_query("UPDATE users SET favorite_songs = '$userFavSongs' WHERE username = '$username'");

			//check to see if the query went through
			if (!mysql_query($sql, $con)){
  					die('Error: ' . mysql_error());
          }

 

But the DB never updates. Anyone see any problems with the syntax? Whole script is:

<?php

//turn on error reporting
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
header('Content-Type: text/xml');

//pull variables
//need to do some error checking here
$username = ($_GET['username']);
$songId = ($_GET['songId']);
$incORdec = ($_GET['incORdec']);

//connect with database
$con = mysql_connect("localhost","root","");
if(!$con){
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("musicneverstopped", $con);
//end connecting to database

//declare placeholders

$result = mysql_query("SELECT favorite_songs FROM users WHERE username = '$username'");

//error check - see if this is safe to remove later
if(!$result){
  die(mysql_error());
  }

//check if any results were returned
if(mysql_num_rows($result) > 0){
  
  while($row = mysql_fetch_array($result)){
    //check if the current songs artist is in the artist array
    $userFavSongs = $row['favorite_songs'];
    }//end while
    $userFavSongsArray = $userFavSongs->getElementsByTagName('song');
    
    if($incORdec == 'inc'){
      $userFavSongs = $userFavSongs . "<song>". $songId . "</song>";
    }
    else{
      foreach($userFavSongsArray as $a){
        if($a == $songId){
          $userFavSongsArray =- $a;
        }//end if
      }//end foreach
      foreach($userFavSongsArray as $b){
          $userFavSongs =+ "<song>" . $b . "</song>";
        }//end foreach
    }//end else   
      
     //update DB 
    $sql = mysql_query("UPDATE users SET favorite_songs = '$userFavSongs' WHERE username = '$username'");


			//check to see if the query went through
			if (!mysql_query($sql, $con)){
  					die('Error: ' . mysql_error());
          }
              
  }//end if
  	  
//////////////////////////////////////////

//close database connection
mysql_close($con);//close mysql connection

    
?>

Link to comment
https://forums.phpfreaks.com/topic/165350-working-with-update/
Share on other sites

Try putting your query in a string and echo it to ensure it contains the proper values:

 

    $sql = "UPDATE users SET favorite_songs = '$userFavSongs' WHERE username = '$username'";
    echo $sql;
    $result = mysql_query("UPDATE users SET favorite_songs = '$userFavSongs' WHERE username = '$username'");
    if (!mysql_query($result, $con)){
        die('Error: ' . mysql_error());
    }

Link to comment
https://forums.phpfreaks.com/topic/165350-working-with-update/#findComment-872001
Share on other sites

There are two mysql_query() statements. The second one is going to fail because it is trying to use the TRUE/FALSE returned by the first one as the query string for the second one.

 

If you are making a first attempt at an UPDATE query, I recommend forgoing the Ajax for now. Get the basics working first.

Link to comment
https://forums.phpfreaks.com/topic/165350-working-with-update/#findComment-872014
Share on other sites

I get the following errors when accessing the page directly (location + setUserFavSongs.php?username=danwoods&songId=tdb2009-01-29s2s02&incORdec=inc as the address)

 

 

Warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/musicneverstopped/php/setUserFavSongs.php:2) in /opt/lampp/htdocs/musicneverstopped/php/setUserFavSongs.php on line 7

 

Fatal error: Call to a member function getElementsByTagName() on a non-object in /opt/lampp/htdocs/musicneverstopped/php/setUserFavSongs.php on line 45

 

 

How would I clear the value of mysql_query()?

Link to comment
https://forums.phpfreaks.com/topic/165350-working-with-update/#findComment-872022
Share on other sites

The 'header error' is probably being caused by output before the header call.  You cannot have any output before a header redirect.  Move that function to be the first line directly after your opening PHP tag (read more here - HEADER ERRORS).

 

As for the second error, I'm not sure what you're trying to do.  I don't even see where you instantiate the object.

 

How would I clear the value of mysql_query()?

 

You don't have to clear the value just don't call 'mysql_query()' twice.  You should assign a variable to the function call so you can check to check if the return value is a resource (success) or FALSE (obviously, failure).  Something like this:

 

    $sql = "UPDATE users SET favorite_songs = '$userFavSongs' WHERE username = '$username'";
    echo $sql;
    $result = mysql_query("UPDATE users SET favorite_songs = '$userFavSongs' WHERE username = '$username'");
    if (!$result){
        die('Error: ' . mysql_error());
    }

Link to comment
https://forums.phpfreaks.com/topic/165350-working-with-update/#findComment-872039
Share on other sites

moving the header function didn't remove the warning, just put it at a different line number.

 

What I'm trying to do is pull the xml stored in the table 'users' and the field 'favorite_songs'. Then either add a song to that xml or take one away.

 

Could I put the queries in different functions in the script, would that help? Do I really need to write a whole different script to handle a single query and a few lines of code? Surely there are times when developers need to do multiple queries in a single function...

Link to comment
https://forums.phpfreaks.com/topic/165350-working-with-update/#findComment-872122
Share on other sites

commenting out those two lines still leaves errors. Does my header look right?

 

New error from commenting out error producing lines:

1Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

Link to comment
https://forums.phpfreaks.com/topic/165350-working-with-update/#findComment-872144
Share on other sites

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.