...

View Full Version : php and mysql linking and amending



chimp
05-21-2004, 11:33 AM
Hi


This is my first post, so Hello.


here's my problem:


I am in the process of building a website located at : www.ilt.bridgwater.ac.uk/~staua



The site is primarily written in html, but I have a couple of mysql table linked to it via php.



Everything is working fine except for one thing:



i am building an admin area which isn't quite finished yet but can still be accessed at:



www.ilt.bridgwater.ac.uk/~staua/Contacts.php



what i am trying to do is as follows:



upon opening the page i want two options to be available,



1. If i don't select an option button it will act as an add new record facility.

2. if an option button is selected then it will act as an amend option. the data of the selected record will then be parsed over to the Amend form for updating



something is clearly not functioning for me and my knowledge of php, is pretty much Zero.



i have the following code which sort of works to a point:



it is founsd at: http://www.ilt.bridgwater.ac.uk/~staua/contactslist.php




contacts list:







html>

<head>

<title>Contacts List</title>

<link rel="stylesheet" href="stylesheet.css" />

</head>

<body>

<?php

$user = "username";

$pass = "password";

$db = "dbname";

$link = mysql_connect("localhost",$user,$pass);

if (!$link) die("Couldn't connect to MySQL server");

mysql_select_db($db, $link) or die("Couldn't open $db: ".mysql_error());

$result = mysql_query("SELECT * FROM Contacts");

$num_rows = mysql_num_rows($result);

print "<h2>RTFC Officials Contact Details</h2>";

print "<form method='post' action='Contacts.php'>\n";

print "<table border=\"1\">\n";

while ($a_row = mysql_fetch_row($result))

{

print "<tr>\n";

if ($a_row[1] == "")

print "\t<td>&nbsp;</td>\n";

else

print "\t<td>$a_row[1]</td>\n";

if ($a_row[2] == "")

print "\t<td>&nbsp;</td>\n";

else

print "\t<td>$a_row[2]</td>\n";

if ($a_row[3] == "")

print "\t<td>&nbsp;</td>\n";

else

print "\t<td>$a_row[3]</td>\n";

if ($a_row[4] == "")

print "\t<td>&nbsp;</td>\n";

else

print "\t<td>$a_row[4]</td>\n";

print "\t<td>$field</td><td><input name='opt' type='radio' value=$a_row[0]></td>\n";

print "</tr>\n";

}

print "</table>\n";

print "<p><input type=\"submit\" value=\"Add A New Record\"></p>\n";

print "</form>\n"; mysql_close($link);?>

</body>

</html>





how this works is that if no radio button is clicked it should act as a new entry is to be added, however if a radio button is used then it will take on the Amend process to alter current data.



when the radio button is activated and the button clicked the following code is used:




Contacts.php









<html>

<head>

<title>RTFC Newsletter</title>

<link rel="stylesheet" type="text/css" href="stylesheet.css">

</head>

<body>

<?php

import_request_variables('p');

if (isset($opt))

{

if (isset($name2) && isset($email2) && isset($position2) && isset($tel2))

{

// check user input here

$dberror = "";

$ret = update_database($name2,$position2,$email2,$tel2,$dberror);

if (!$ret)

print "Error: $dberror<br />";

else

print "<h2>Thank you very much, $name has been added </h2>";

}

else

{

$user = "username";

$pass = "password";

$db = "dbname";

$link = mysql_connect("localhost",$user,$pass);

if (!$link) die("Couldn't connect to MySQL server");

mysql_select_db($db, $link) or die("Couldn't open $db: ".mysql_error());

$result = mysql_query("SELECT * FROM Contacts WHERE ID='".$pos."'");

$num_rows = mysql_num_rows($result);

$a_row = mysql_fetch_row($result);

print "numrows = '$num_rows'";

write_form2($a_row[1], $a_row[2], $a_row[3], $a_row[4]);

}

}

else

{

if (isset($name) && isset($email) && isset($position) && isset($tel))

{

// check user input here

$dberror = "";

$ret = add_to_database($name,$position,$email,$tel,$dberror);

if (!$ret)

print "Error: $dberror<br />";

else

print "<h2>Thank you very much, $name has been added </h2>";

}

else

write_form();

}





function add_to_database($name, $position, $email, $tel, &$dberror)

{

$user = "username";

$pass = "password";

$db = "dbname";

$link = mysql_connect("localhost",$user,$pass);

if (!$link)

{

$dberror = "Couldn't connect to MySQL server";

return false;

}

if (!mysql_select_db($db, $link))

{

$dberror = mysql_error();

return false;

}

$query="INSERT INTO Contacts (Name, Position, Email, Tel)

VALUES('$name', '$position', '$email', '$tel' )";

if (!mysql_query($query, $link))

{

$dberror = mysql_error();

return false;

}

return true;

// mysql_close($link);

}



function update_database($name2, $position2, $email2, $tel2, &$dberror)

{

$user = "username";

$pass = "password";

$db = "dbname";

$link = mysql_connect("localhost",$user,$pass);

if (!$link)

{

$dberror = "Couldn't connect to MySQL server";

return false;

}

if (!mysql_select_db($db, $link))

{

$dberror = mysql_error();

return false;

}

$query="UPDATE Contacts SET Name='$name2', Position='$position2', Email='$email2', Tel='$tel2'

WHERE ID='$pos'";

if (!mysql_query($query, $link))

{

$dberror = mysql_error();

return false;

}

return true;

// mysql_close($link);

}



function write_form()

{

global $PHP_SELF;

print "<form action=\"$PHP_SELF\"method=\"POST\">\n";

print "<p><h2>Please Add Details As Required</h2></p>";

print "<form action=\"$PHP_SELF\" method=\"POST\">\n";

print " <p> Officials Name</p>\n";

print "<input type=\"text\" name=\"name\">";

print " <p> Officials Position</p>\n";

print "<input type=\"text\" name=\"position\">";

print " <p> Your e-mail address</p>\n";

print "<input type=\"text\" name=\"email\">";

print " <p> Officials Tel</p>\n";

print "<input type=\"text\" name=\"tel\">";

print "<p><input type=\"submit\" value=\"Register\"></p>\n</form>\n";

print "";

}



function write_form2($name2, $position2, $email2, $tel2)

{

global $PHP_SELF;

print "name2 = '$name2'";

print "<form action=\"$PHP_SELF\"method=\"POST\">\n";

print "<p><h2>Please Ammend Details As Required</h2></p>";

print "<form action=\"$PHP_SELF\" method=\"POST\">\n";

print " <p> Officials Name</p>\n";

print "<input type=\"text\" name=\"name2\" value=$name2>";

print " <p> Officials Position</p>\n";

print "<input type=\"text\" name=\"position2\" value=$position2>";

print " <p> Your e-mail address</p>\n";

print "<input type=\"text\" name=\"email2\" value=$email2>";

print " <p> Officials Tel</p>\n";

print "<input type=\"text\" name=\"tel2\" value=$tel2>";

print "<input type=\"hidden\" name=\"pos\" value=$pos>";

print "<p><input type=\"submit\" value=\"Register\"></p>\n</form>\n";

print "";

}

?>

<? print "opt = '$opt'"; ?>

</body>

</html>





the trouble i am having is that i want the data that is attached to the relevant radio button to be copied across to the fields for ammending, but they are not being transferred.



i am really desperate for some help here please, i am even prepared to give you access to my site if that help.



Andy

sad69
05-21-2004, 08:58 PM
The error you're getting is T_STRING on line 32. So there's something wrong on line 32... I'm just not sure which line that is. T_STRING usually means that you haven't terminated a string (ie. you opened a quote, but never closed it..)

Here are the lines that look questionable to me anyway:

$ret = update_database($name2,$position2,$email2,$tel2,$d berror);
$ret = add_to_database($name,$position,$email,$tel,$dberr or);


These might just be copy+paste errors, but I just saw them.. but look for quote errors around line 32 and see what you come up with.

Good luck,
Sadiq.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum