PHP - trying to automate/loop a save function

Posted by Homer_J on Stack Overflow See other posts from Stack Overflow or by Homer_J
Published on 2010-04-28T13:07:24Z Indexed on 2010/05/06 12:08 UTC
Read the original article Hit count: 136

Filed under:

EDIT: the difference between the pages are the simply $q1, $q2, $q3 becomes, for example, $q13, $q14, $q15 - they are questions in the form.

Hi all,

I have the following code:

$rguid = $_POST["r"];
$ip=substr($_SERVER['REMOTE_ADDR'], 0, 50);
$browser=substr($_SERVER['HTTP_USER_AGENT'], 0, 255);   

$q1 = $_POST["q1"];
$q2 = $_POST["q2"];
$q3 = $_POST["q3"];
$q4 = $_POST["q4"];
$q5 = $_POST["q5"];
$q6 = $_POST["q6"];
$q7 = $_POST["q7"];
$q8 = $_POST["q8"];

$respondent_id = decode_respondent_guid($rguid);
$rcount=respondent_status($respondent_id);

if ($rcount==0) {

    $proc = mysqli_prepare($link, "INSERT INTO tresults (respondent_id, ip, browser, q1, q2, q3, q4, q5, q6, q7, q8) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
    mysqli_stmt_bind_param($proc, "issiiiiiiii", $respondent_id, $ip, $browser, $q1, $q2, $q3, $q4, $q5, $q6, $q7, $q8);
    mysqli_stmt_execute($proc);
    $mysql_error = mysqli_error($link);
    if ($mysql_error!="") {
        printf("Unexpected database error: %s\n", $mysql_error);
        mysqli_stmt_close($proc);
        mysqli_clean_connection($link);
        exit();
    } else
    {
        mysqli_stmt_close($proc);
        mysqli_clean_connection($link);
        update_completion_status($respondent_id, 'Started');
        header("Location: page2.php?r=".$rguid);
    }
}

This code works a treat - no problem.

At the moment, that code appears on a 'save' page that relates to a 'form' page (i.e. if I have 5 .php pages with a form on each, I need 5 save.php pages to save each pages data to the database) to save data to my SQL database. What I would like is to have a single save.php page that will work with as many 'form' pages as I have. So instead of manually having to setup and change each save page, it automates it.

I've no idea of the coding to do this, though I suspect something like a foreacg or loop etc.

Any suggestions?

Thanks,

Homer.

© Stack Overflow or respective owner

Related posts about php