Welcome! Log In Create A New Profile

Advanced

2008 project

Posted by bothajar 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
2008 project
May 13, 2009 11:19AM
Mac

I am busy putting the different parts of my project together and now feel much more confident as previously about PHP. It is starting to make much more sense. I went step by step through the course and the relevant part of the textbook to builsd up a thorough knowledge before redoing the project.

I have however succeeded in loosing some of my work as a result of hardware failure. Fortunately that deals only with the update of records.

I include my code below. I have done it differently in my previous attempt, but cannot remember what I have done. My question deals with the transfer of variables between amend1.php and amend2.php. I think there is a problem, but I cannot find it. I can see form the feedback from amend2.php that no data has been transferred from amend1.php to amend2.php. The feedback is the following:

Amending record .................(Here we expect the id of the record after the string)
Record Updated ..................(Here we also expect the id of the record between the two words)

Search on ...................(Here we also expect the id of the record after the string)

Please advise me. Here is the code:


Language: PHP
[b]amend1.php[/b]   <?PHP session_start(); ?> <HTML>   <?php $record = $_POST[';record';]; echo "Course id: $record<br><BR>"; $cname=cname; printf($_POST[';cname';]);   //Connecting to MYSQL $con = mysql_connect("localhost","root",""); if (!$con) { die(';Could not connect: '; . mysql_error()); }   mysql_select_db("registration", $con);   //Select the database we want to use mysql_select_db("registration") or die("Could not find database");   $result=mysql_query(" SELECT * FROM course WHERE cid=';$record';"); $num=mysql_num_rows($result); $i=0; while ($i < $num) {   // collect all details for our one reference $cid=mysql_result($result,$i,"cid"); $cname=mysql_result($result,$i,"cname");   $f=';<font face=Verdana,Arial,Helvetica size=2 Color=Blue';;   ?>   <TABLE WIDTH="100%" CELLPADDING="10" CELLSPACING="0" BORDER="2"> <TR ALIGN="center" VALIGN="top"> <TD ALIGN="center" COLSPAN="2" ROWSPAN="1" BGCOLOR="#F2F2F2">   <FORM ACTION="amend2.php" METHOD=POST>   <P ALIGN="LEFT"> Course ID: <INPUT TYPE="text" cid="ud_cid" VALUE="<? echo "$record" ?>">   <P ALIGN="LEFT"> Course name: <INPUT TYPE="text" cname="ud_cname" Size="50" VALUE="<? echo "$cname" ?>">   </B><BR><BR> </P><P><INPUT TYPE="Submit" VALUE="Update the Record" NAME="Submit"> </P></FORM></TD></TR></TABLE>   <? ++$i; } ?>       [b]Amend2.php[/b]   ?PHP session_start(); ?> <?php $ud_cid=$_POST[';ud_cid';]; $ud_cname=$_POST[';ud_cname';]; if ($ud_cid = "") echo "! No identifier retrieved"; else echo "Amending record $ud_cid";   //clean up any carriage returns etc //$ud_cname = preg_replace("/[\n\r]*/","",$ud_cname);   //Connecting to MYSQL $con = mysql_connect("localhost","root",""); if (!$con) { die(';Could not connect: '; . mysql_error()); }     //Select the database we want to use mysql_select_db("registration", $con) or die("Could not select database"); mysql_query(" UPDATE course SET cname=';$ud_cname'; WHERE cid=';$ud_cid';"); echo "<BR>Record $ud_cid Updated<BR><BR>"; ?>   <?php //if you want to check it';s ok, display new data echo "Search on $ud_cid"; echo "<BR>"; //Connecting to MYSQL $con = mysql_connect("localhost","root",""); if (!$con) { die(';Could not connect: '; . mysql_error()); }   mysql_select_db("registration", $con) or die("Could not select database");   $q="SELECT * FROM course WHERE cid =';$ud_cid';"; $result = mysql_query( $q, $con ) or die(" - Failed More Information:<br> <pre>$q</pre><br>Error: " . mysql_error()); $num_rows = mysql_num_rows($result);   echo "<table border=0>\n"; echo "<tr><td></td><td></td><td></td><td></td></tr>\n";   do { printf("<tr><td>%s</td><td>%s</td>\n", $myrow["cid"], $myrow["cname"]); } while ($myrow = mysql_fetch_array($result)); echo "</table>\n"; //else { //echo "Sorry, no records were found"; //} mysql_free_result($result); mysql_close(); session_destroy(); ?>
Re: 2008 project
May 14, 2009 08:41AM
Have you fixed your errors? Did you come right or do you still need help?
avatar
Mac
Re: 2008 project
May 14, 2009 10:37AM
<input type= .... is not defined correctly.

There must be... name=ud_cid
Re: 2008 project
May 14, 2009 11:42AM
just noticed some things in your code:

Language: PHP
mysql_select_db("registration", $con);   //Select the database we want to use mysql_select_db("registration") or die("Could not find database");

Why are you selecting the database twice?

Language: PHP
$cname=cname;

This doesnt look right. I think it should be something like $cname = $_POST['cname'];

Language: PHP
$result=mysql_query(" SELECT * FROM course WHERE cid=';$record';"); $num=mysql_num_rows($result); $i=0; while ($i < $num) {   // collect all details for our one reference $cid=mysql_result($result,$i,"cid"); $cname=mysql_result($result,$i,"cname");

I dont think this while statement is neccessary since cid is a primary key your mysql query should never return more than 1 row.
Re: 2008 project
May 14, 2009 09:38PM
Stephany no I have checked over and over tonight and cannot find the incorrect INPUT statement. And the other solutions offered just nicified the code but made no difference to the results. everything is working except, as I have indicated amend2.php where it is not displaying or changing anything and I assume it has something to do with transferring of the data from amend1.php.

I have applied all advice but cannot see the wrong INPUT statement. I have even put name=ud_cid into it and also name="ud_cid" and nothing has changed. I still get the same final output. So silly I know there is something that I am overlooking I just cannot allocate it.

I would love any further advice.

I have also started redoing the php files by using another approach in an attempt to get something that is working and then try to find the fault.

Johan
Re: 2008 project
May 15, 2009 10:59AM
Hi Johan

I have found your problem in amend2. Use the $_GET to pass the variables and then you should come right. Below I have inserted where you need to make the changes. Read up in chapter 2 on page 42. It explains how to use the $_GET and $_POST quite nicely!

Remember that you should also try and practice indentations and standards when writing your code. It makes it easier reading.

Note also that ?PHP and <? is incorrect - you MUST use <?php to be able to access the php code, otherwise it is just recognised as plain HTML text and does nothing.

<?php
session_start();
?>
<?php
$ud_cid=$_POST['ud_cid'];
$ud_cname=$_POST['ud_cname'];
if ($_GET['ud_cid']= ""winking smiley echo "! No identifier retrieved";
else
echo "Amending record" . $ud_cid;



Hope this helps!
avatar
Mac
Re: 2008 project
May 15, 2009 01:59PM
The name=ud_cid part MUST be in.

The Form method is POST, so you must retrieve with $_POST. You use $_GET when you have a link like this <a href='page.php?ud_cid=$ud_cid'>Send ID</a>

The error checking order is - make sure $record exist on 1st page.
Then make sure the db query has retrieved the cid and cname.
Then add them to the form as follows: <INPUT TYPE="text" name="ud_cid" VALUE="<? echo "$record" ?>"> etc.
Send to the next page.
Catch them with &_POST and then echo them e.g. ud_cid on top of the page before you start using it.
Always use error checking like this to narrow the problem down.

$cname=cname, although wrong as pointed out is a leftover I imagine that is not used, and is correctly called lower down so it does not matter - but you should remove it since it is visual clutter. Loop is also 100% correct with his while comment - but you can use while for now - it will not have any effect on execution time. I always say to beginners - get the code working, and then you can get smart smiling smiley

However, you should not be defining you db connection on these pages - it should be in a single file which you include on every page you use it in. If you leave it like this, I will have to go and change your connection details on every pages before I can use it with my db. I don't want to do that. I want to go to one single page and change my connection details there, to be valid throughout your application. It figure that you can also include the mysql_select_db statement there since we are using a single datatbase.
Re: 2008 project
May 15, 2009 09:52PM
Stephany thank you for the advice and I will also try to use the indentations and standards correctly because I agree that as it is, it is very difficult to read. I will test the corrected script and post a message here to tell you of the results.

Johan
Re: 2008 project
May 15, 2009 09:53PM
Thank you Mac. You have provided extremely valuable guidelines. it is getting easier as I go along and lean more and more each day.

Johan
Re: 2008 project
May 15, 2009 10:30PM
Re: 2008 project
May 28, 2009 11:19AM
I have tried to apply file detele.php in chapter 6 of the prescribed book. If I try to run that program I get the following message:

Parse error: parse error, unexpected '!', expecting T_STRING or T_VARIABLE or '$' in C:\Xampp\xampp\htdocs\php textbook code\chapter 06\delete2.php on line 7

The relevant extract from the code is indicated below. The relevant line (7) referred to is if (!isset(!isset($_GET['do']) || $_GET['do'] != 1) || $_GET['do'] != 1) {

Language: PHP
<?php $link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error()); mysql_select_db(';moviesite';, $link) or die ( mysql_error()); // DELETE SCRIPT if (!isset(!isset($_GET[';do';]) || $_GET[';do';] != 1) || $_GET[';do';] != 1) { ?> <p align="center" style="color:#FF0000"> Are you sure you want to delete this <?php echo $_GET[';type';]; ?>?<br> <a href="<?php echo $_SERVER[';REQUEST_URI';]; ?>&do=1">yes</a> or <a href="index.php">Index</a> </p>


I have tried to avoid this code, but I can find no other acceptable code to delete relational data in more than one table.

The only other easy alternative is to use the innoDB engine in MySQL, but that was not our instruction and table setup so I assume that that is unchangeable. Does anyone perhaps have advice. I have up til mow just deleted a record per table at a time but that is not a good way to approach a relationship database.
Re: 2008 project
May 28, 2009 01:10PM
Hi !
What is the due date for the submission of the project?
Mthimbana
avatar
Mac
Re: 2008 project
May 28, 2009 01:10PM
Line 7 is not as in the book?


It is ( !isset($_GET['do']) || $_GET['do'] != 1) {

Try and understand the code:

!isset($_GET['do']) means the value of $GET['do'] is not set
|| means or
$_GET['do'] != 1 means the value of $GET['do'] is not one

Try and read you line 7 as here above and see if it makes sense - besides the brackets not being equal!
Re: 2008 project
May 28, 2009 02:24PM
O thank you very much, it is working now. I looked in the book and copied the file from the publisher's site and did not notice the difference. I understand it well now and now it makes a lot of sense. It was short-sighted of me not to spot the difference :-(.. I either need new glasses or should not work late night or juts be more accurate smile
Thanks
Re: 2008 project
June 05, 2009 08:20AM
Mac

My project is finished. I am just adding some hyperlinks, headers, security and finishing touches. I will finish it this weekend. Will it still be fine? I am proud that at last everything is working and I have learned an amazing lot. Thank your for the wonderful course and guidelines.

Johan
avatar
Mac
Re: 2008 project
June 05, 2009 09:19AM
Johan
Fine, but I must have it by Monday morning 08h00, as I will be uploading marks onto the system. I will not have an opportunity to do so thereafter....
Re: 2008 project
June 09, 2009 01:36AM
Sorry but I got the response not as normally through my gmail. and only now opened it. I have been working hard on the project and as you can see it is now 1:28 am and last night also went to bed at about 3 am. I am just struggling with one page and query and I cannot find the problem. It is the page for the class list per module. I had it working previously but now it is corrupt and I cannot find the mistake. Everything else is working.

Does this now mean that I have failed? Toby last year gave me space until a few days before the marks had to be in. Is there no possibility anymore?

I have learned a lot and picked up skills, but I wished to be able to show that on paper.

Regards

Johan Botha
avatar
Mac
Re: 2008 project
June 09, 2009 04:02PM
I have marked all (except 3 which I have sent back to fix one or two things related to the config.php page - they include connection details on every page and I am not going to change it on every page - they get -10% already) and will now put final marks in this Thursday - please, by 07h00 it must be in. You know how tedious the process is to get marks added after the cut-off, and I will not go in again before the cut-off date smiling smiley
Sorry, only registered users may post in this forum.

Click here to login