Jump to content

[SOLVED] Help - If Statement


DarkPrince2005

Recommended Posts

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>";
};
?>

Link to comment
https://forums.phpfreaks.com/topic/77917-solved-help-if-statement/
Share on other sites

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>";
}
}
?>

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>";
?>

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?

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?

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>";;

};

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.