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.

Where is the mistake in here?

Discussion in 'PHP' started by baris22, Feb 16, 2007.

  1. #1
    Hello all. This code is not working. Where is the mistake?
    Thanks.

    
    
    <?php
    ob_start();
    session_start();
    
    $hostname="x";
    $username="x";
    $password=" x";
    $dbname="x";
    
    $con = mysql_pconnect ($hostname,$username,$password) or print "No database connection exists or invalid query<br>".mysql_error();
    mysql_select_db ($dbname, $con) or print "Can't select databse <br>".mysql_error();
    
    if(isset($_REQUEST['add']) && $_REQUEST['add']=="1") 
    {
        $desc = str_replace("[", "\r\n", $_REQUEST['description']);
        $arrayOflinks = explode(";", $_REQUEST['links']);
    
        // Get Extension of first extry
        $extPos = strrpos($arrayOflinks[0], ".");
        if ($extPos !== false)
        {
            $ext = substr($arrayOflinks[0], $extPos+1);
            $extL = strtolower($ext);
            if (!strcasecmp($extL, "bmp") || !strcasecmp($extL, "jpg") || !strcasecmp($extL, "gif"))
            {
                $imageTag = "<img src='".$arrayOflinks[0]."' border='0'>";
                $arrayOflinks[0]=$imageTag;
            }
        }
        $link = implode("<br>", $arrayOflinks);
    
    
        $query = "insert into articles (`id`, `type`, `title`, `description`, `links`) values('0', '".$_REQUEST['type']."','".$_REQUEST['title']."','".$desc."','".$link."')";
        print $query;
        mysql_query($query);
        exit();
    }
    
        $result = mysql_query("select * from articles");
        if ($result != "")
        {
            $z = mysql_num_rows($result);
            for ($i = 0 ; $i < $z ; $i++)
            {
                $row = mysql_fetch_array($result);
                print $row['type'].", ".$row['title'].", ".$row['description'].", ".$row['links']."<br>";
            } 
        }
    
    ?>
    
    
    Code (markup):

    This one is working.

    
    
    <?php
    ob_start();
    session_start();
    
    $hostname="x";
    $username="x";
    $password=" x";
    $dbname="x";
    
    $con = mysql_pconnect ($hostname,$username,$password) or print "No database connection exists or invalid query<br>".mysql_error();
    mysql_select_db ($dbname, $con) or print "Can't select databse <br>".mysql_error();
    
    if(isset($_REQUEST['add']) && $_REQUEST['add']=="1") 
    {
        $desc = str_replace("[", "\r\n", $_REQUEST['description']);
        $query = "insert into articles (`id`, `quality`, `type`, `title`, `description`, `links`, `owner`) values('0', '0000000000', '".$_REQUEST['type']."','".$_REQUEST['title']."','".$desc."','".$_REQUEST['links']."', '".$_REQUEST['owner']."')";
        print $query;
        mysql_query($query);
        exit();
    }
    
        $result = mysql_query("select * from articles");
        if ($result != "")
        {
            $z = mysql_num_rows($result);
            for ($i = 0 ; $i < $z ; $i++)
            {
                $row = mysql_fetch_array($result);
                print $row['type'].", ".$row['title'].", ".$row['description'].", ".$row['links']."<br>";
            } 
        }
    
    ?>
    
    Code (markup):
     
    baris22, Feb 16, 2007 IP
  2. Choller

    Choller Peon

    Messages:
    388
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What error are you getting? That would make this much easier.
     
    Choller, Feb 16, 2007 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I do not get any error. It is just not adding anything to the database.
     
    baris22, Feb 16, 2007 IP
  4. Choller

    Choller Peon

    Messages:
    388
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Put this in your script.

    if (!$result) {
       die('Invalid query: ' . mysql_error());
    }
    PHP:
    You will probably get some detailed info on what is going wrong if somethign goes wrong

    Also, do
    echo $query;
    PHP:
    You will see the sql and you can check if there is anything wrong there.

    I'd like to see what $query echo's.
     
    Choller, Feb 16, 2007 IP
  5. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    But there is nothing wrong with results.

    The problem is it is not adding anything to the database.
     
    baris22, Feb 16, 2007 IP
  6. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #6
    Are you posting data to this script?
    You should use $_POST - if data is posted.
    You should use $_GET - if data is in the URL.

    You shouldnt really use $_REQUEST.

    Edit as appropriate and see if it works.
     
    papa_face, Feb 16, 2007 IP
  7. Choller

    Choller Peon

    Messages:
    388
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Is the "id" field the unique key (autoincrement)?
     
    Choller, Feb 16, 2007 IP
  8. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #8
    I am using $_REQUEST.
    Because it is connected to a software. I do not have any chance to edit the software.
     
    baris22, Feb 16, 2007 IP
  9. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #9
    You dont have to. All you have to know is if it is being posted or not....
    Post the form that you fill in to add data to this db.
     
    papa_face, Feb 16, 2007 IP
  10. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #10
    Yes, it is.
     
    baris22, Feb 16, 2007 IP
  11. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #11
    Then you shouldnt be inserting a id
    Try this
    
        $query = "insert into articles (`type`, `title`, `description`, `links`) values('".$_REQUEST['type']."','".$_REQUEST['title']."','".$desc."','".$link."')";
        print $query;
        mysql_query($query);
        exit();
    
    Code (markup):
     
    papa_face, Feb 16, 2007 IP
  12. Choller

    Choller Peon

    Messages:
    388
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Also do tell us print $query; is saying. If you say that the code works but it isn't inserting that there must be something with the $query string
     
    Choller, Feb 16, 2007 IP
  13. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #13
    I meant i do not have a chance to check it. Because it is the software i am using to submit the article. so i can not check your code. if i do i will not get any answer.
     
    baris22, Feb 16, 2007 IP
  14. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #14
    So if you cant help us, how are we expected to help you?
     
    papa_face, Feb 16, 2007 IP
  15. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #15
    When i use this i get an error saying Invalid query:

    Nothing else.

    Because there is no query.




     
    baris22, Feb 16, 2007 IP
  16. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #16
    It did not work.

     
    baris22, Feb 16, 2007 IP
  17. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #17
    You are obviously not familiar with php. Choller told you to put it into not make a page with that code in.
    Saying it did not work doesnt help us.
    Put this code AT THE TOP of your script (that means inside of the <?php)
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    Code (markup):
    and tell us what errors you get.
     
    papa_face, Feb 16, 2007 IP
  18. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #18

    You got me wrong...

    Ofcource i added that code in the php codes.

    I am saying: There is no way i will get an error message while i am trying to insert something to the database.
    Because i am not adding anything manually. This page is getting called by the software.

    The software includes this page while you are submitting.

    So can you tell me how i am going to check if the query working or not.

    There is only one way i can see. If it is added to the database, it means it worked. If it is not added, it means it did not work.
     
    baris22, Feb 16, 2007 IP
  19. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #19
    Do what I said and give us an error! We cant help without an error. If there are no errors displayed when you put in the code i mentioned ^^ there are no errors.
     
    papa_face, Feb 16, 2007 IP
  20. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #20
    papa face and Choller

    Thanks very much for your help. But it does not work. I do not get any error messages.

    It is just not adding anything.
     
    baris22, Feb 16, 2007 IP