Russia Posted November 6, 2009 Share Posted November 6, 2009 I currently have this script: <?php $date2 = date("F j Y"); $ip = $_SERVER['REMOTE_ADDR']; require("inc/config.php"); $sql="INSERT INTO `accounts` (username, password, ip, addeddate) VALUES('$_POST[username]','$_POST[Password]','$ip','$date2')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } echo ""; $result = mysql_query("SELECT email FROM admin WHERE id = '1'"); if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; } $row = mysql_fetch_row($result); $to = $row[0]; $qcheck = "SELECT * FROM admin"; $rcheck = mysql_query($qcheck) or die("<B>MySQL error! Make sure you edited the config file! <BR><BR> Error: ".mysql_error()); $frow = mysql_fetch_array($rcheck); if ($frow['sendemail'] == "1") { $subject = "New Registered User"; $from = "test"; $message = " A new user has signed up and has been added to the database Username: $_POST[username] Password: $_POST[Password] IP Address: $ip Date: $date2 "; $headers = "From: $to"; $sent = mail($to, $subject, $message, $headers) ; echo "sent"; } else { echo "not sent - since not enabled"; } ?> It stores the information in a database and then sends me an email. You can choose if you want to send the email with if ($frow['sendemail'] == "1") if its 1 then send, if 2 or anything else then don't send. I was wondering if things can be taken out or combined to shorten the script. I am not sure what to do. To me it seems very long for something so simple. Thank You. Link to comment https://forums.phpfreaks.com/topic/180612-simplifying-a-script/ Share on other sites More sharing options...
Russia Posted November 6, 2009 Author Share Posted November 6, 2009 Bump. Link to comment https://forums.phpfreaks.com/topic/180612-simplifying-a-script/#findComment-952899 Share on other sites More sharing options...
Cardale Posted November 6, 2009 Share Posted November 6, 2009 I didn't test it just fixed it up real fast. <?php $date2 = date("F j Y"); $ip = $_SERVER['REMOTE_ADDR']; require("inc/config.php"); $sql="INSERT INTO `accounts` (username, password, ip, addeddate) VALUES('$_POST[username]','$_POST[Password]','$ip','$date2')"; mysql_query($sql) or die('Error: ' . mysql_error(); $result = mysql_query("SELECT email FROM admin WHERE id = '1'") or die('Could not run query: ' . mysql_error()); $row = mysql_fetch_row($result); $to = $row[0]; $qcheck = "SELECT * FROM admin"; $rcheck = mysql_query($qcheck) or die("<B>MySQL error! Make sure you edited the config file! <BR><BR> Error: ".mysql_error()); $frow = mysql_fetch_array($rcheck); if ($frow['sendemail'] == "1"){ $subject = "New Registered User"; $from = "test"; $message = " A new user has signed up and has been added to the database Username: $_POST[username] Password: $_POST[Password] IP Address: $ip Date: $date2 "; $headers = "From: $to"; $sent = mail($to, $subject, $message, $headers) ; echo "sent"; }else{ echo "not sent - since not enabled"; } ?> Link to comment https://forums.phpfreaks.com/topic/180612-simplifying-a-script/#findComment-952900 Share on other sites More sharing options...
Russia Posted November 6, 2009 Author Share Posted November 6, 2009 I get this error: Parse error: syntax error, unexpected ';' in /home/cherdak/public_html/email.php on line 6 <?php $date2 = date("F j Y"); $ip = $_SERVER['REMOTE_ADDR']; require("inc/config.php"); $sql="INSERT INTO `accounts` (username, password, ip, addeddate) VALUES('$_POST[username]','$_POST[Password]','$ip','$date2')"; mysql_query($sql) or die('Error: ' . mysql_error(); $result = mysql_query("SELECT email FROM admin WHERE id = '1'") or die('Could not run query: ' . mysql_error()); $row = mysql_fetch_row($result); $to = $row[0]; $qcheck = "SELECT * FROM admin"; $rcheck = mysql_query($qcheck) or die("<B>MySQL error! Make sure you edited the config file! <BR><BR> Error: ".mysql_error()); $frow = mysql_fetch_array($rcheck); if ($frow['sendemail'] == "1"){ $subject = "New Registered User"; $from = "test"; $message = " A new user has signed up and has been added to the database Username: $_POST[username] Password: $_POST[Password] IP Address: $ip Date: $date2 "; $headers = "From: $to"; $sent = mail($to, $subject, $message, $headers) ; echo "sent"; }else{ echo "not sent - since not enabled"; } ?> Link to comment https://forums.phpfreaks.com/topic/180612-simplifying-a-script/#findComment-952901 Share on other sites More sharing options...
huszi001 Posted November 7, 2009 Share Posted November 7, 2009 You missed one ) in die function mysql_query($sql) or die('Error: ' . mysql_error()); And you need check $_POST['Username'] and $_POST['Password'] is empty or not before your script starts + use mysql_real_escape_string() or it can be injected. <?php if (isset($_POST['Username']) && isset($_POST['Password'])) { $date2 = date("F j Y"); $ip = $_SERVER['REMOTE_ADDR']; require("inc/config.php"); $sql="INSERT INTO `accounts` (username, password, ip, addeddate) VALUES('".mysql_real_escape_string($_POST[username])."','".mysql_real_escape_string($_POST[Password])."','$ip','$date2')"; mysql_query($sql) or die('Error: ' . mysql_error()); $result = mysql_query("SELECT email FROM admin WHERE id = '1'") or die('Could not run query: ' . mysql_error()); $row = mysql_fetch_row($result); $to = $row[0]; $qcheck = "SELECT * FROM admin"; $rcheck = mysql_query($qcheck) or die("<B>MySQL error! Make sure you edited the config file! <BR><BR> Error: ".mysql_error()); $frow = mysql_fetch_array($rcheck); if ($frow['sendemail'] == "1"){ $subject = "New Registered User"; $from = "test"; $message = " A new user has signed up and has been added to the database Username: $_POST[username] Password: $_POST[Password] IP Address: $ip Date: $date2 "; $headers = "From: $to"; $sent = mail($to, $subject, $message, $headers) ; echo "sent"; }else{ echo "not sent - since not enabled"; } }else{ echo "Empty username or password!"; } ?> Link to comment https://forums.phpfreaks.com/topic/180612-simplifying-a-script/#findComment-953039 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.