1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

SQL statement

Discussion in 'MySQL' started by dean5000v, May 8, 2008.

  1. #1
    ok well i've created a table using this code:

    
    
    
    CREATE TABLE `stock`(
    `item_id` INT NOT NULL AUTO_INCREMENT,
    `item_code` VARCHAR(20) NOT NULL ,
    `item_desc` VARCHAR(60) NOT NULL ,
    `item_stock` INT(20) NOT NULL ,
    PRIMARY KEY (`item_id`));
    
    
    Code (markup):
    ive created a form where the user inserts a search code for item_code,
    all i wont is the sql to search item_code value and then echo the row onto the page.

    this is the SQl i attempted:

    $query = "SELECT * FROM $mysql_table WHERE item_code = $item_id";
    $result = mysql_query($query) or die(mysql_error());
    Code (markup):
    but all i get is Resource id #3 when i echo $result
     
    dean5000v, May 8, 2008 IP
  2. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    here is a update to the code ive done, but something is still wrong...

    <?php
    $search_db = htmlspecialchars(addslashes($_POST['search_db']), ENT_QUOTES);
    
    if(isset($_POST['search_db'])) {
    if(!$item_search) {
    
    echo "No search entered, please go back and fill in the fields properly.";
    }
    else {
    $query = mysql_query("SELECT * FROM stock WHERE item_code LIKE '%$search_db%'");
    $resultnum = mysql_num_rows($query);
    
    if($resultnum>0) { 
    while($row=mysql_fetch_array($query)) {
    echo "$row";
    }
    }
    }
    
    }
    ?>
    Code (markup):
     
    dean5000v, May 8, 2008 IP
  3. Bezzen

    Bezzen Peon

    Messages:
    244
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The $row is an array, so I guess that's why you can't echo it directly.

    Try a little something like this:

    while($row = mysql_tech_array($query)) {
    $item_id = $row['item_id'];
    $item_code = $row['item_code'];
    etc.

    echo "$item_id $item_code<br>";
    }
     
    Bezzen, May 8, 2008 IP
  4. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok ive got my code working fine now !!!!


    just one more thing, i wont to be able to update the fields,

    item_desc, item_code, item_stock

    so some sql using the update statement that will update into these 3 groups on the same row. i suk at mysql !!!!
     
    dean5000v, May 8, 2008 IP
  5. bluecape

    bluecape Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    mysql_query("UPDATE stock SET item_desc = $item_desc, item_code = $item_code, item_stock = $item_stock, WHERE item_id = $item_id");

    if i understand your question correctly
     
    bluecape, May 8, 2008 IP
  6. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ok this is the code i used,

    i dont get any errors or nothing i know everything works in it apart from the sql,


    if(isset($_POST['update_db'])) {
    if(!$item_code) {
    echo "<span class=\"red\">Please fill the item code field</span>";
    }
    else if(!$item_desc) {
    	echo"<span class=\"red\">Please fill in the item description field</span>";  
    }
    else if (!$item_stock || $item_stock > 100) {
     echo '<span class="red">Stock must be a number between [1 - 100]</span>';
    } 
    else {
    
    $sql=("UPDATE stock SET item_desc = $item_desc, item_stock = $item_stock, WHERE item_code = $item_code");
    	
    	 	 $result=mysql_query($sql);
    
    
    } 
    Code (markup):
    can anything think what could be wrong ??
     
    dean5000v, May 8, 2008 IP
  7. bluecape

    bluecape Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i think you forgot to put another bracket '}' after the else statement.
     
    bluecape, May 8, 2008 IP
  8. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    nope there is another bracket just havent pasted it in, if i did miss a bracket i wld get errors.
     
    dean5000v, May 8, 2008 IP
  9. bluecape

    bluecape Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    try this version of code

    
    <?php
    if(isset($_POST['update_db'])) 
    {
    	$item_code = $_POST['item_code'];
    	$item_desc = $_POST['item_desc'];
    	$item_stock = $_POST['item_stock'];
    	
    	if(!$item_code || !$item_desc || !$item_stock || ($item_stock > 100)) 
    	{
    		if(!$item_code)
    		echo "<span class=\"red\">Please fill the item code field</span><br>";
    		if(!$item_desc)
    		echo"<span class=\"red\">Please fill in the item description field</span><br>";
    		if (!$item_stock || $item_stock > 100)
    		echo "<span class=\"red\">Stock must be a number between [1 - 100]</span><br>";
    	}
    	else 
    	{
    		$sql=("UPDATE stock SET item_desc = $item_desc, item_stock = $item_stock, WHERE item_code = $item_code");
    		$result=mysql_query($sql);
    	}
    
    }
    ?>
    
    Code (markup):
     
    bluecape, May 8, 2008 IP
  10. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    nope that code doesnt work either, i know its deffently the mysql code not working because i echod back the variables and worked fine, so if this is the structure of my table:

    CREATE TABLE `stock`(
    `item_id` INT NOT NULL AUTO_INCREMENT,
    `item_code` VARCHAR(20) NOT NULL ,
    `item_desc` VARCHAR(60) NOT NULL ,
    `item_stock` INT(20) NOT NULL ,
    PRIMARY KEY (`item_id`));
    
    
    Code (markup):
    and im using this update command

    
    $sql=("UPDATE stock SET item_desc = $item_desc, item_stock = $item_stock, WHERE item_code = $item_code");
    		$result=mysql_query($sql);
    
    Code (markup):
     
    dean5000v, May 8, 2008 IP
  11. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    could it be because item_code isnt my primary key and item_id is ?
     
    dean5000v, May 8, 2008 IP
  12. bluecape

    bluecape Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    not neccessarily, but it could be.

    try to add this line at the end of the script and see what it says:
    print mysql_error();
     
    bluecape, May 8, 2008 IP
  13. Agent_Smith

    Agent_Smith Well-Known Member

    Messages:
    890
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    145
    #13
    Try..

    $sql= "
    UPDATE stock 
    SET item_desc = '$item_desc', 
    item_stock = '$item_stock '
    WHERE item_code = '$item_code'";
    		
    $result = mysql_query($sql) or die(mysql_error());
    PHP:
     
    Agent_Smith, May 8, 2008 IP
  14. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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 'WHERE item_code = 111' at line 1
     
    dean5000v, May 8, 2008 IP
  15. bluecape

    bluecape Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    the variable, $item_desc. does it has any special chars in it? like " or ' or anything similar? a small thing like this could also mess up mysql queries.
     
    bluecape, May 8, 2008 IP
  16. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Agent_Smith, i could possibly kiss you haha, thanks alot works now. knew it was the sql :D and thanks to bluecape for helping me aswel !!!!
     
    dean5000v, May 8, 2008 IP
  17. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    nah wasn't any characters screwing it up, i escaped all dangerous charactors, was just the sql syntex :D
     
    dean5000v, May 8, 2008 IP
  18. Agent_Smith

    Agent_Smith Well-Known Member

    Messages:
    890
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    145
    #18
    No problem.

    You needed some ' in the variables.
     
    Agent_Smith, May 8, 2008 IP
  19. bluecape

    bluecape Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #19
    glad u found the answer to your problem dean5000v. and thank you agent_smith for helping us out. really appreciated it. :)
     
    bluecape, May 8, 2008 IP