ukferret Posted April 18, 2007 Share Posted April 18, 2007 I have the code for inserting data to a database form a form (Code below) but I want the data to be entered upon a page loading, how would I do this? <?php require_once('Connections/connect.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO members (username, last_date) VALUES (%s, %s)", GetSQLValueString($_POST['username'], "text"), GetSQLValueString($_POST['last_date'], "text")); mysql_select_db($database_connect, $connect); $Result1 = mysql_query($insertSQL, $connect) or die(mysql_error()); $insertGoTo = "index2.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form method="post" name="form1" action="<?php echo $editFormAction; ?>"> <input type="hidden" name="username" value="<? php echo session_MM_Username ?>" size="32" /> <input name="submit" type="submit" value="Insert record" /> <input type="hidden" name="last_date" value="<?php echo date("j, n, Y"); ?>"> <input type="hidden" name="MM_insert" value="form1"> </form> <p> </p> </body> </html> Link to comment https://forums.phpfreaks.com/topic/47609-insert-to-database-on-page-load-help/ Share on other sites More sharing options...
Psycho Posted April 18, 2007 Share Posted April 18, 2007 Not tested, could be some syntax errors: <?php require_once('Connections/connect.php'); if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $insertSQL = sprintf("INSERT INTO members (username, last_date) VALUES (%s, %s)", GetSQLValueString(session_MM_Username, "text"), GetSQLValueString(date("j, n, Y"), "text")); mysql_select_db($database_connect, $connect); $Result1 = mysql_query($insertSQL, $connect) or die(mysql_error()); $insertGoTo = "index2.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } ?> Link to comment https://forums.phpfreaks.com/topic/47609-insert-to-database-on-page-load-help/#findComment-232578 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.