Help, PHP MySQL Script

Discussion in 'PHP' started by yokowasis, Aug 4, 2010.

  1. #1
    Is there a way to make a "universal" php script which works on any form ?

    Example case :
    Page A :

    [Form ]
    Name :
    Age :

    [Submit to page B]

    Page B :
    Insert into Mysql Name and Age.

    I want to know if there is some way to make Page B works for any submitted form, so it doesn't necessarily Name and Age. But the action still same, Insert New Record on MySQL.
     
    yokowasis, Aug 4, 2010 IP
  2. belltower

    belltower Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It's doable. PHP is very good at reflection. But it probably wouldn't be easy. You'd have to have some context, as in, what type of data should 'age' hold? What table should it go into? And so on. You could do it with a clever use of classes. If you're looking for this sort of thing, though, I'd recommend trying out a framework like CodeIgniter (with the DataMapper library) or Zend. Might as well take advantage of work that's already been done.
     
    belltower, Aug 4, 2010 IP
  3. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    
    $fields = "INSERT INTO table (";
    $vals = " VALUES (";
    foreach ($_POST as $v => $a) {
     $fields .= "`$v`,";
     $vals .= "'$a',";
    }
    //clean up comers at the end, there's probably a nicer way then this, but this one came to mind
    $fields = substr($fields,0,strlen($fields)-1);
    $vals = substr($vals,0,strlen($vals)-1);
    //close off statement
    $fields .= ")";
    $vals .=  ")";
    mysql_query($fields.$vals);
    
    PHP:
    should do the trick, you'll want to escape the posted values to avoid injection, though should work
     
    themullet, Aug 4, 2010 IP
  4. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #4
    could even post table name and then unset it before the post loop
     
    themullet, Aug 4, 2010 IP
  5. yokowasis

    yokowasis Member

    Messages:
    141
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    30
    #5
    Okay, the code seems good. The code basically parsing the field and value into mysql insert script. But how to use it? Can you give me the example on my case ?
     
    yokowasis, Aug 4, 2010 IP
  6. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #6
    put the code into a script file i.e. adder.php and make the form side point something like <form action="adder.php" method="post">
     
    Last edited: Aug 5, 2010
    themullet, Aug 5, 2010 IP
  7. Tom_Lee

    Tom_Lee Peon

    Messages:
    192
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    can u talking like auto fill form?
     
    Tom_Lee, Aug 5, 2010 IP
  8. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #8
    for the form would be something like
    
    echo '<form action="adder.php" method="post">';
    $qid = mysql_query("DESCRIBE ".$table);
    while ($res = mysql_fetch_array($qid)) {
    	echo "<input name=\"$res[Field]\" type=\"text\"/>"<br/>";
    }
    echo '<input type="submit" value="submit" name="submit" />';
    echo "</form>";
    
    PHP:
     
    themullet, Aug 5, 2010 IP