Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Apr 2006
    Location
    Tadley, UK
    Posts
    63
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Confusing Myself

    Hello,

    I seem to be having a bit of trouble getting this code to work:
    PHP Code:
    foreach($_POST as $n => $h) {
            
    $h mysql_real_escape_string($h);
            
    $n '$h';
            echo 
    $n.$h.'<br />';    
            } 
    What I want is for example:
    $_POST['name'] = 'Dave';
    I want $n to be name, and $h to be Dave, so I can use $name to insert Dave into the db.

    Thanks if you can help

    Steven

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    must be:
    PHP Code:
    foreach($_POST as $n => $h) {
            
    $h mysql_real_escape_string($h);
            
    $n $h;
       
    // same thing in a single line:
       
    $n mysql_real_escape_string($h);
       
    // because $n = $h, this will be: 'DaveDave<br />'
       // to be 'nameDave<br/>' you must remove $n = $h
            
    echo $n.$h.'<br />';    

    in this construct:
    PHP Code:
    $n '$h'
    both $h and $n are variable, so is ok to write as $n = $h. Also the variable included between ' are not expanded.

    regards
    Last edited by oesxyl; 08-07-2008 at 01:39 AM.

  • #3
    New Coder
    Join Date
    Apr 2006
    Location
    Tadley, UK
    Posts
    63
    Thanks
    5
    Thanked 0 Times in 0 Posts
    thanks for your post.

    I did try this earlier, and for some reason it didn;t work. WOuld that script set $name = 'dave' each time so I can use it on a MySQL statement after the foreach?

    PHP Code:
    <?php
    // This is the main user 'dashboard'
    if($_COOKIE['loggedin'] != && !isset($_COOKIE['userName'])) {
        
    header('location:index.php?do=login');
    }
    foreach(
    $_POST as $n => $h) {
        if(
    $h == '' || $h == "") {
            
    $error[] = "&bull; {$n} has no data in.";
        }
    }
    if(
    sizeof($error) > 0) {
        foreach(
    $error as $msg
            echo 
    $msg;
        include(
    "newproject.php");
        die();
    } else {
        foreach(
    $_POST as $n => $h) {
            
    $h mysql_real_escape_string($h);
            
    $n '$h';
            
    //echo $n.$h.'<br />';    
            
    }
            
    //die();
        
    }
        
    $query mysql_query("INSERT INTO `projects` (`id`,`name`, `started`, `deadline`, `status`, `type`, `notes`, `cname`, `cemail`, `cphone`, `cspreq`) VALUES ('', '$name', '$started', '$deadline', '', '$type', '$notes', '$cname', '$cemail', '$cphone', '$spreq')") or die(mysql_error());
        if(
    $query == FALSE) {
            echo 
    "There was an error with {$query}, If this problem persists, Please contact the server administrator.";
        } else {
            
    header('location:index.php?do=showprojects');
        }
    ?>
    The script above running how it is now inserts blank rows in the db. However earlier, I managed to get the variables to be set, so it must be something to do with that and inserting them into the query.

    Thankyou
    Last edited by Webmonkey; 08-07-2008 at 01:40 AM.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Webmonkey View Post
    thanks for your post.
    you are welcome,

    PHP Code:
    <?php
    // This is the main user 'dashboard'
    if($_COOKIE['loggedin'] != && !isset($_COOKIE['userName'])) {
        
    header('location:index.php?do=login');
    }
    // add this line to see what have in $_POST
    print_r($_POST);
    foreach(
    $_POST as $n => $h) {
    //    if($h == '' || $h == "") {
            
    if($h == ''){ // same thing as above
            
    $error[] = "&bull; {$n} has no data in.";
        }
    }
    if(
    sizeof($error) > 0) {
        foreach(
    $error as $msg
            echo 
    $msg;
        include(
    "newproject.php");
        die();
    } else {
        foreach(
    $_POST as $n => $h) {
            
    $h mysql_real_escape_string($h);
    //        $n = '$h';
            //echo $n.$h.'<br />';    
                    
    echo $n.': '.$h.'<br/>';
            }
            
    //die();
        
    }
            
    // you must check all this value if exists and have the values you expect
        
    $query mysql_query("INSERT INTO `projects` (`id`,`name`, `started`, `deadline`, `status`, `type`, `notes`, `cname`, `cemail`, `cphone`, `cspreq`) VALUES ('', '".$name."', '".$started."', '".$deadline."', '', '".$type."', '".$notes."', '".$cname."', '".$cemail."', '".$cphone."', '".$spreq."')") or die(mysql_error());
        if(
    $query == FALSE) {
            echo 
    "There was an error with {$query}, If this problem persists, Please contact the server administrator.";
        } else {
            
    header('location:index.php?do=showprojects');
        }
    ?>
    Edit: read the comments and look to the query, is modified
    regards

  • Users who have thanked oesxyl for this post:

    Webmonkey (08-07-2008)

  • #5
    New Coder
    Join Date
    Apr 2006
    Location
    Tadley, UK
    Posts
    63
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks for your reply, I copy and pasted the code, however it still didn't work. Here is the MySQK table:

    PHP Code:
    --
    -- 
    Table structure for table `projects`
    --

    CREATE TABLE `projects` (
      `
    idtinyint(3NOT NULL auto_increment,
      `
    nametext NOT NULL,
      `
    startedint(15NOT NULL,
      `
    deadlineint(15NOT NULL,
      `
    statustinyint(1NOT NULL,
      `
    typevarchar(225NOT NULL,
      `
    noteslongtext NOT NULL,
      `
    cnametext NOT NULL,
      `
    cemailvarchar(225NOT NULL,
      `
    cphonevarchar(11NOT NULL,
      `
    cspreqtext NOT NULL,
      
    PRIMARY KEY  (`id`)
    ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

    --
    -- 
    Dumping data for table `projects`
    --

    INSERT INTO `projects` (`id`, `name`, `started`, `deadline`, `status`, `type`, `notes`, `cname`, `cemail`, `cphone`, `cspreq`) VALUES
    (1'Tadley Computers'1111111111110'CMS''Nm''Tadley PC''email@tadleypc.com''29323890'''),
    (
    2''000''''''''''''),
    (
    3''000''''''''''''); 
    Thanks

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Webmonkey View Post
    Thanks for your reply, I copy and pasted the code, however it still didn't work.
    what is the output of the line with print_r ?

    regards

  • Users who have thanked oesxyl for this post:

    Webmonkey (08-07-2008)

  • #7
    New Coder
    Join Date
    Apr 2006
    Location
    Tadley, UK
    Posts
    63
    Thanks
    5
    Thanked 0 Times in 0 Posts
    with S for example in the th efields, print_r the query seems to retrurn :
    [php]Array ( [name] => S [started] => s [deadline] => S [type] => S [notes] => S [cname] => S [cemail] => S [cphone] => S [spreq] => S [submit] => Go ) S: S[/]php]

  • #8
    New Coder
    Join Date
    Apr 2006
    Location
    Tadley, UK
    Posts
    63
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I have fixed this error now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •