Need help with two of my PHP scripts.
The first one I need help with is my addnewrecord.php script. Here is the code:
[code]
Tyne Arts & Ents - Add New Record <?php /* Make the connection to the database. The syntax is: variable = mysql_connect ('hostname', 'username', 'password') ; */ $con = mysql_connect('hostname', 'username', 'password') or die("Could Not Connect to the Database!"); mysql_select_db("username"); $tle = $_POST['Title']; $artname = $_POST['ArtistName']; $desc = $_POST['Description']; $tipe = $_POST['Type']; $sddate = $_POST['StartDate']; $eddate = $_POST['EndDate']; $emit = $_POST['Time']; $vnuename = $_POST['VenueName']; $vnueadd = $_POST['VenueAddress']; $tel = $_POST['Telephone']; $web = $_POST['URL']; $sql="INSERT INTO events (Title, ArtistName, Description, Type, StartDate, EndDate, Time, VenueName, VenueAddress, Telephone, URL) VALUES ('$tle','$artname','$desc','$tipe','$sddate','$eddate','$emit','$vnuename','$vnueadd','$tel','$web')"; if (strlen($tle)>25) { echo "You may only enter upto 25 characters in the Title field"; } if (strlen($artname>)30) { echo "You may only enter upto 30 characters in the ArtistName field"; } if (strlen($desc)>200) { echo "You may only enter upto 200 characters in the Description field"; } if (strlen($vnuename)>50) { echo "You may only enter upto 50 characters in the VenueName field"; } if (strlen($vnueadd)>70) { echo "You may only enter upto 70 characters in the VenueAddress field"; } if (strlen($tel)>11) { echo "You may only enter upto 11 characters in the Telephone field"; } if (strlen($url)>40) { echo "You may only enter upto 40 characters in the URL field"; } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } if ($tle <= 25 && $artname <= 30 && $desc <= 200 && $vnuename <= 50 && $vnueadd <= 70 && $tel <=11 && $url<=40 } { echo "1 record added"; } mysql_close($con) ?>[/code]Basically I want some validation. When all the validation is correct then the echo message “1 record added” appears but all I get at the moment is a blank screen when clicking the submit button on the form. The code does work when I remove all the validation codes. I’ve tried putting the number values in " " but still didn’t work and now I’m out of ideas. I have shown this code to someone else before and they said the mysql_query needs to be within the if {brackets} below it, otherwise it will always execute even if there are errors with the input validation but I didn’t know what he meant. They were also the ones that said I needed the strlen command.
Also how do I add validation for the time and date? would it be something like:
if $sdate !=date
{
echo “You must enter a valid date format”
}
and:
if $emit !=time
{
echo “You must enter a valid date format”
}
Second script I need help with is my deleterecord.php. Bascially, the events in the database appear in a dropdown box and the user selects which event they would like to delete. So far I’ve just edited my editrecord.php, editrecordv2.php and editrecordv3.php script so that it deletes and doesn’t update a record but maybe i’m doing it in the wrong way. At the moment whenever I try to delete an event I get the following error message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM events WHERE title = 'terewr'' at line 1
terewr was just a random title name I did as a test. Here’s my php code for my deleterecord.php:
[code]
Tyne Arts & Ents - Delete RecordSelect an event to delete
<?php/*
Make the connection to the database. The syntax is:
variable = mysql_connect ('hostname', 'username', 'password') ;
*/
$con = mysql_connect(‘hostname’, ‘username’, ‘password’) or die(“Could Not Connect to the Database!”);
mysql_select_db(“username”); //select which database to use
$sql = "SELECT Title FROM events";
$queryresult = mysql_query($sql) or die (mysql_error());
while($row = mysql_fetch_array($queryresult)){
$tle = $row['Title'];
echo "<option value = "$tle">$tle</option> n";
}
mysql_free_result($queryresult);
mysql_close($con);
?>
[/code]deleterecordv2.php code:
[code]
Tyne Arts & Ents - Delete Record Continued... <?php/*
Make the connection to the database. The syntax is:
variable = mysql_connect ('hostname', 'username', 'password') ;
*/
$con = mysql_connect(‘webhost’, ‘username’, ‘password’) or die(“Could Not Connect to the Database!”);
mysql_select_db(“username”); //select which database to use
$code = $_POST[‘event’]; // store the users choice of event from the web form
$sql = “Delete * FROM events WHERE title = ‘$code’”;
$queryresult = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_array($queryresult); // Fetch the record returned by the query
/* store the record details returned by the SQL query in variables.
Note: no need to use a while loop as there will only be one record returned by the query */
$tle = $row[‘Title’];
$artname = $row[‘ArtistName’];
$desc = $row[‘Description’];
$tipe = $row[‘Type’];
$sddate = $row[‘Startdate’];
$eddate = $row[‘Enddate’];
$emit = $row[‘Time’];
$vnuename = $row[‘VenueName’];
$vnueadd = $row[‘VenueAddress’];
$tel = $row[‘Telephone’];
$web = $row[‘URL’];
$eventcode = $row[‘EventCode’];
mysql_free_result($queryresult);
mysql_close($con);
?>
Delete chosen event
" />Title | " /> |
ArtistName | " maxlength = "30" /> |
Description | " maxlength = "200" /> |
Type | " maxlength = "20" /> |
Startdate | " /> |
Enddate | " /> |
Time | " /> |
VenueName | " maxlength = "20" /> |
VenueAddress | " maxlength = "70" /> |
Telephone | " maxlength = "11" /> |
URL | " maxlength = "70" /> |
deletrecordv3.php code:
[code]
Tyne Arts & Ents - Delete Event Continued... <? $code = $_POST['EventCode']; // need this to identify the particular record $Ndesc = $_POST['Ndescription']; // new value for description $Ntype = $_POST['Ntype']; // new value for type $Nsdate = $_POST['Nsdate']; // new value for start date $Nedate = $_POST['Nedate']; // new value for end date $Ntime = $_POST['Ntime']; // new value for the time $Nvnuename = $_POST['Nvnuename']; // new value for the venue name $Nvnueadd = $_POST['Nvnueadd']; // new value for the venue address $Ntel = $_POST['Ntel']; // new value for the telephone number $Nurl = $_POST['Nurl']; // new value for the url echo "Deleting record ..."; include 'database_conn.php'; // make db connection $con = mysql_connect('webhost', 'username', 'password') or die("Could Not Connect to the Database!"); mysql_select_db("username"); $sql = "DELTE * FROM events WHERE EventCode = $code"; mysql_query($sql) or die (mysql_error()); echo "Event deleted"; mysql_close($con); ?>
Go Back to Admin page [/code]
I know it’s a lot of code to look through but any help will be really appreciated.