DarkPrince2005 Posted November 19, 2007 Share Posted November 19, 2007 Can anybody help me on how i would change the attached code to just save the filled in fields to the database if a file is not uploaded? <?php ini_set('display_errors','On'); error_reporting(E_ALL); ini_set('upload_max_filesize','5M'); ini_set('max_execution_time','180'); ini_set('max_input_time','180'); mysql_connect("localhost","root",""); mysql_select_db("npa"); if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = mysql_query("INSERT INTO case_management (date, applicant, respondent, case_number, judge, representative, offence, outcome, relief, occupation, region, status, case_summary, name, size, type, content ) ". "VALUES ('$_POST[date]','$_POST[applicant]','$_POST[respondent]','$_POST[case_number]','$_POST[judge]','$_POST[representative]','$_POST[offence]','$_POST[outcome]','$_POST[relief]','$_POST[occupation]','$_POST[region]','$_POST[status]',\"$_POST[case_summary]\",'$fileName', '$fileSize', '$fileType', '$content')"); //mysql_query($query) or die('Error, query failed'); echo "<script language=JavaScript>window.location='add.html'</script>"; } else { $sql = mysql_query("INSERT INTO case_management (date, applicant, respondent, case_number, judge, representative, offence, outcome, relief, occupation, region, status, case_summary, name, size, type, content ) VALUES ('$_POST[date]','$_POST[applicant]','$_POST[respondent]','$_POST[case_number]','$_POST[judge]','$_POST[representative]','$_POST[offence]','$_POST[outcome]','$_POST[relief]','$_POST[occupation]','$_POST[region]','$_POST[status]',\"$_POST[case_summary]\",'', '', '', '','')"); echo "<script language=JavaScript>window.location='add.html'</script>"; }; ?> Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 19, 2007 Share Posted November 19, 2007 Try this <?php ini_set('display_errors','On'); error_reporting(E_ALL); ini_set('upload_max_filesize','5M'); ini_set('max_execution_time','180'); ini_set('max_input_time','180'); mysql_connect("localhost","root",""); mysql_select_db("npa"); if(isset($_POST['upload']) // submit button is pressed { if($_FILES['userfile']['size'] > 0) // file is being selected { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = mysql_query("INSERT INTO case_management (date, applicant, respondent, case_number, judge, representative, offence, outcome, relief, occupation, region, status, case_summary, name, size, type, content ) ". "VALUES ('$_POST[date]','$_POST[applicant]','$_POST[respondent]','$_POST[case_number]','$_POST[judge]','$_POST[representative]','$_POST[offence]','$_POST[outcome]','$_POST[relief]','$_POST[occupation]','$_POST[region]','$_POST[status]',\"$_POST[case_summary]\",'$fileName', '$fileSize', '$fileType', '$content')"); //mysql_query($query) or die('Error, query failed'); echo "<script language=JavaScript>window.location='add.html'</script>"; } else { $sql = mysql_query("INSERT INTO case_management (date, applicant, respondent, case_number, judge, representative, offence, outcome, relief, occupation, region, status, case_summary, name, size, type, content ) VALUES ('$_POST[date]','$_POST[applicant]','$_POST[respondent]','$_POST[case_number]','$_POST[judge]','$_POST[representative]','$_POST[offence]','$_POST[outcome]','$_POST[relief]','$_POST[occupation]','$_POST[region]','$_POST[status]',\"$_POST[case_summary]\",'', '', '', '','')"); echo "<script language=JavaScript>window.location='add.html'</script>"; } } ?> Quote Link to comment Share on other sites More sharing options...
DarkPrince2005 Posted November 19, 2007 Author Share Posted November 19, 2007 It's still not saving to the database if I don't upload a file <?php ini_set('display_errors','On'); error_reporting(E_ALL); ini_set('max_execution_time','180'); ini_set('max_input_time','180'); mysql_connect("192.168.42.112:3306","npa","fa11en"); //mysql_connect("localhost","root",""); mysql_select_db("npa"); if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = mysql_query("INSERT INTO case_management (date, applicant, respondent, case_number, judge, representative, offence, outcome, relief, occupation, region, status, case_summary, name, size, type, content ) ". "VALUES ('$_POST[date]','$_POST[applicant]','$_POST[respondent]','$_POST[case_number]','$_POST[judge]','$_POST[representative]','$_POST[offence]','$_POST[outcome]','$_POST[relief]','$_POST[occupation]','$_POST[region]','$_POST[status]',\"$_POST[case_summary]\",'$fileName', '$fileSize', '$fileType', '$content');"); } else { $sql = mysql_query("INSERT INTO case_management (date, applicant, respondent, case_number, judge, representative, offence, outcome, relief, occupation, region, status, case_summary, name, size, type, content ) VALUES ('$_POST[date]','$_POST[applicant]','$_POST[respondent]','$_POST[case_number]','$_POST[judge]','$_POST[representative]','$_POST[offence]','$_POST[outcome]','$_POST[relief]','$_POST[occupation]','$_POST[region]','$_POST[status]',\"$_POST[case_summary]\",'', '', '', '','');"); }; echo "<script language=JavaScript>window.location='add.html'</script>"; ?> Quote Link to comment Share on other sites More sharing options...
aschk Posted November 19, 2007 Share Posted November 19, 2007 Does this line work : $sql = mysql_query("INSERT INTO case_management (date, applicant, respondent, case_number, judge, representative, offence, outcome, relief, occupation, region, status, case_summary, name, size, type, content ) VALUES ('$_POST[date]', '$_POST[applicant]', '$_POST[respondent]', '$_POST[case_number]', '$_POST[judge]', '$_POST[representative]','$_POST[offence]', '$_POST[outcome]', '$_POST[relief]', '$_POST[occupation]', '$_POST[region]', '$_POST[status]',\"$_POST[case_summary]\",'', '', '', '','');"); how about you do this : $sqlString = "INSERT INTO case_management (date, applicant, respondent, case_number, judge, representative, offence, outcome, relief, occupation, region, status, case_summary, name, size, type, content ) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','', '', '', '','')"; $sql = sprintf($sqlString,$_POST[date], $_POST[applicant], $_POST[respondent], $_POST[case_number], $_POST[judge], $_POST[representative], $_POST[offence], $_POST[outcome], $_POST[relief], $_POST[occupation], $_POST[region], $_POST[status], $_POST[case_summary]); echo "This is my sql string".$sql; if($execSql = mysql_query($sql)){ echo "it worked"; } else { echo "IT FAILED".mysql_error(); } Your sql just might be incorrect. Do you require default fields? Quote Link to comment Share on other sites More sharing options...
DarkPrince2005 Posted November 21, 2007 Author Share Posted November 21, 2007 I tried the above code and it failed, it said that the number of fields in the code doesn't match the number of fields in the database, but I checked it 7 times and they do match. But what exactly do you mean by default fields? Quote Link to comment Share on other sites More sharing options...
DarkPrince2005 Posted November 22, 2007 Author Share Posted November 22, 2007 here's the working bit: if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = mysql_query("INSERT INTO case_management (date, applicant, respondent, case_number, commisioner, representative, region, offence, charge, dispute, outcome, relief, status, case_summary, name, size, type, content ) ". "VALUES ('$_POST[date]','$_POST[applicant]','$_POST[respondent]','$_POST[case_number]','$_POST[commisioner]','$_POST[representative]','$_POST[region]','$_POST[offence]','$_POST[charge]','$_POST[dispute]','$_POST[outcome]','$_POST[relief]','$_POST[status]',\"$_POST[case_summary]\",'$fileName', '$fileSize', '$fileType', '$content');"); echo "<script language=JavaScript>window.location='add.html'</script>"; }else { mysql_query("INSERT INTO case_management (date, applicant, respondent, case_number, commisioner, representative, region, offence, charge, dispute, outcome, relief, status, case_summary, name, size, type, content ) ". "VALUES ('$_POST[date]','$_POST[applicant]','$_POST[respondent]','$_POST[case_number]','$_POST[commisioner]','$_POST[representative]','$_POST[region]','$_POST[offence]','$_POST[charge]','$_POST[dispute]','$_POST[outcome]','$_POST[relief]','$_POST[status]',\"$_POST[case_summary]\",'', '', '', '');"); echo "<script language=JavaScript>window.location='add.html'</script>";; }; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.