Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script Execution question

Status
Not open for further replies.

Netwrkengeer

IS-IT--Management
Apr 4, 2001
184
US
I'am trying to write a php script to access a mysql database. I have a small script that connects to the database that lets me add and delete stuff manualy. But I want the script to automatically execute at 1:00AM, then grab info from "Table A" in the database and dump it into "Table B" in the same database. Do you know of a script or method of doing this.

thanks
Robert.

And here is the script I wrote.

<html>

<body>



<?php



$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);

mysql_select_db(&quot;JBHdb&quot;,$db);



if ($submit) {

// here if no ID then adding else we're editing

if ($id) {

$sql = &quot;UPDATE users SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id&quot;;

} else {

$sql = &quot;INSERT INTO users (first,last,address,position) VALUES ('$first','$last','$address','$position')&quot;;

}

// run SQL against the DB

$result = mysql_query($sql);

echo &quot;Record updated/edited!<p>&quot;;

} elseif ($delete) {

// delete a record

$sql = &quot;DELETE FROM users WHERE id=$id&quot;;

$result = mysql_query($sql);

echo &quot;$sql Record deleted!<p>&quot;;

} else {

// this part happens if we don't press submit

if (!$id) {

// print the list if there is not editing

$result = mysql_query(&quot;SELECT * FROM users&quot;,$db);

while ($myrow = mysql_fetch_array($result)) {

printf(&quot;<a href=\&quot;%s?id=%s\&quot;>%s %s</a> \n&quot;, $PHP_SELF, $myrow[&quot;id&quot;], $myrow[&quot;first&quot;], $myrow[&quot;last&quot;]);

printf(&quot;<a href=\&quot;%s?id=%s&delete=yes\&quot;>(DELETE)</a><br>&quot;, $PHP_SELF, $myrow[&quot;id&quot;]);

}

}



?>

<P>

<a href=&quot;<?php echo $PHP_SELF?>&quot;>ADD A RECORD</a>

<P>

<form method=&quot;post&quot; action=&quot;<?php echo $PHP_SELF?>&quot;>

<?php



if ($id) {

// editing so select a record

$sql = &quot;SELECT * FROM users WHERE id=$id&quot;;

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);

$id = $myrow[&quot;id&quot;];

$first = $myrow[&quot;first&quot;];

$last = $myrow[&quot;last&quot;];

$address = $myrow[&quot;address&quot;];

$position = $myrow[&quot;position&quot;];

// print the id for editing



?>

<input type=hidden name=&quot;id&quot; value=&quot;<?php echo $id ?>&quot;>

<?php

}



?>

First name:<input type=&quot;Text&quot; name=&quot;first&quot; value=&quot;<?php echo $first ?>&quot;><br>

Last name:<input type=&quot;Text&quot; name=&quot;last&quot; value=&quot;<?php echo $last ?>&quot;><br>

Address:<input type=&quot;Text&quot; name=&quot;address&quot; value=&quot;<?php echo $address ?>&quot;><br>

Position:<input type=&quot;Text&quot; name=&quot;position&quot; value=&quot;<?php echo $position ?>&quot;><br>

<input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Enter information&quot;>

</form>



<?php



}



?>



</body>

</html>
 
It is possible to run PHP scripts from a cron job also, but you would need either a method to call as if being called from a browser (eg.. &quot;wget or, if PHP is installed as a CGI program, you can just execute it as a shell script. For this kind of method, though, you can't use any &quot;user interaction&quot; HTML elements on the page such as a form submit button.

You really don't need PHP to do this. You just need a simple shell script, which you run as a cron job once a night. The script could export the data from table A as a CVS file, delete all records from the table, then import the CVS data into table B. Basically, it would just be a text file with the two commands, as if they were run from the command line, and referencing text files with the actual SQL in them:
Code:
##!/bin/bash
mysql -u username pPassword < /path/to/dir export_script.sql
mysql -u username pPassword < /path/to/dir import_script.sql
The export script would use the SELECT INTO OUTFILE syntax ( to select data from table A and export it to a text file, delimited however you see fit, then after that it would contain the DELETE FROM command.

The import script would run the LOAD DATA INFILE syntax ( to load the data from the textfile into table B.

It might be a good idea to run LOCK TABLES before and UNLOCK TABLES after, also. (
 
OK maybe I'm approaching this the wrong way, maybe you can help me. the objective is to setup an online calendar where a &quot;consultant&quot; will sign up. and an &quot;employer&quot; will sign up. like monster.com. But the difference is they are scheduled to work automatically. where if the &quot;Consultants&quot; qualifications match up with the &quot;employers&quot; requirements then they are scheduled for a date. The date is posted on the calendar, the consultant and/or employee logs in they view the calendar. and the position is filled. OK I know very little about programming. can you tell me how I would make this work, and/or direct me to the resources to accomplish this type of thing.

Right now I’m using Php on a freeBSD server running Apache, and MySql on a win95 machine.
 
That could be a somewhat complicated app. I still don't see why you are deleting from one table to store in another, though.

I would think that you should have a table with employers and specifications, a table with employees and qualifications, then in your PHP code, you do some sort of select and compare, and if the qualifications match, you write a record to a third table, which just contains employee ID's matched with Employer ID's, and the date and time info you need.

Beyond that, it's hard to give you specific advice until I get more specific questions. I recommend the best thing for you is to go read some tutorials that walk you through creating a web-based app with a relational database approach. The ones at are nice and to the point, and I believe has had a few good ones also. has some more advanced ones.
 
Thanks, I will look at them.

The reason for the deletion is I need to set up an exclusion table, so that if 5 people match the criteria for the same job I can run the program for the &quot;best profit scenario&quot; because these people will also specify how far they will travel for work. Meaning that some one who will travel 100 miles can fit the description of 5 jobs and another person who will travel 50 miles will match for 1 job out of the same 5. Meaning if the 100 mile person takes the 50 mile persons potential job, I loss the profit for the 50 mile person because he will not work. So in conclusion, I will not delete the employee table just copy them to a different table, and from their transfer to the exclusion table, then delete.
 
I see your point, but still, a truly relational database generally makes it unnecessary to do such clunky operations. After reading a few relational database tutorials, you'll probably see how you can set up another schema, that uses data more efficiently. Generally, it's rarely necessary to remove data from one table, and place in another table, except for specific performance issues. Your overal design should make it possible for every type of information to exist in ONLY one place, and any qualifying, excluding, differentiating should be done with additional fields or key matching data on separate tables optimized just for that specific information.
 
I see your point, I think you are right. Thanks,
Do you have any suggestions on where I might find code similar to what I'm doing. A code to help me match and compare data, and sort and place it.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top