Tags: concatenate, declared, example, form, hold, html, php, programming, script, string, variable, writing

concatenate string

On Programmer » PHP

6,872 words with 5 Comments; publish: Thu, 15 Nov 2007 18:07:00 GMT; (20094.24, « »)

I have an html form that is writing data to a PHP script. In that script, I have declared a variable to hold the data. An example of the string is:

$writetofile = $oclc . "\t" . "" . "\t" . $main . "\t" . "" . "\t" . $libtype;

This results in a blank screen when I submit the form and the next steps are not performed, which includes actually writing the data to a file and then uploading it to the table. I think the last 2 steps work okay, but I can't seem to get the data to concatenate correctly. I am wondering if there is a problem with the syntax, possibly with the null "" values.

Any suggestions for a modification?

All Comments

Leave a comment...

  • 5 Comments
    • You can simplify it to:

      $writetofile = $oclc . "\t\t" . $main . "\t\t" . $libtype;

      Of course, $oclc, $main and $libtype must exist.

      #1; Fri, 16 Nov 2007 10:10:00 GMT
    • Thanks for your reply. I have made the change you suggested. However, I still can't get this to work. I only get a blank screen. What is supposed to happen when the form is submitted is that the data is written to file and then input into the database from a system call. At one time, I had it writing to file and the database, but with many edits, I am now stuck again. Here is the code:

      <?php

      $mysql_user="dummy";

      $mysql_password="dummy";

      $database="dummy";

      $host="dummy";

      $oclc = $_POST['oclc'];

      $fscskey = $_POST['FSCSKEY'];

      $main = $_POST['main'];

      $libtype = $_POST['libtype'];

      $acadtype = $_POST['acadtype'];

      $acadpubpriv = $_POST['acadpubpriv'];

      $instname = $_POST['instname'];

      $libname = $_POST['LIBNAME'];

      $address = $_POST['ADDRESS'];

      $mailadd = $_POST['mailadd'];

      $city = $_POST['CITY'];

      $state = $_POST['state'];

      $zip = $_POST['ZIP'];

      $zip4 = $_POST['ZIP4'];

      $studentbody = $_POST['pop'];

      $libphone = $_POST['PHONE'];

      $extension = $_POST['extension'];

      $fax = $_POST['FAX'];

      $email = $_POST['email'];

      $web = $_POST['web'];

      $autocoop = $_POST['autocoop'];

      $othercoop = $_POST['coop'];

      $libdirfirst = $_POST['libdirfirst'];

      $libdirlast = $_POST['libdirlast'];

      $libdirphone = $_POST['libdirphone'];

      $libdirext = $_POST['libdirext'];

      $libdiremail = $_POST['libdiremail'];

      $dirjobtitle = $_POST['dirjobtitle'];

      $alamlsfte = $_POST['alamlsfte'];

      $otherlibfte = $_POST['otherlibfte'];

      $totlibfte = $_POST['totlibfte'];

      $totopexp = $_POST['totopexp'];

      $totvolumes = $_POST['totvolumes'];

      $mondayopen = $_POST['mondayopen'];

      $mondayclosed = $_POST['mondayclosed'];

      $tuesopen = $_POST['tuesopen'];

      $tuesclosed = $_POST['tuesclosed'];

      $wedopen = $_POST['wedopen'];

      $wedclosed = $_POST['wedclosed'];

      $thursopen = $_POST['thursopen'];

      $thursclosed = $_POST['thursclosed'];

      $friopen = $_POST['friopen'];

      $friclosed = $_POST['friclosed'];

      $satopen = $_POST['satopen'];

      $satclosed = $_POST['satclosed'];

      $sunopen = $_POST['sunopen'];

      $sunclosed = $_POST['sunclosed'];

      $illtoown = $_POST['illtoown'];

      $illtoinspec = $_POST['illtoinspec'];

      $illtoinstother = $_POST['illtoinstother'];

      $illtoout = $_POST['illtoout'];

      $illtototal = $_POST['illtototal'];

      $illfromown = $_POST['illfromown'];

      $illfrominspec = $_POST['illfrominspec'];

      $illfrominother = $_POST['illfrominother'];

      $illfromout = $_POST['illfromout'];

      $illfromtotother = $_POST['illfromtotother'];

      // Establish connection and select database

      mysql_connect($host,$username,$password);

      mysql_select_db($database) or die( "Unable to select database");

      // Write form values to file - prepare for loading to table

      $writetofile = $oclc . "\t\t" . $main . "\t\t" . $libtype . "\t" . $acadtype . "\t" . "$acadpubpriv . "\t" . $instname . "\t" . $libname . "\t" . $address . "\t" . $mailadd . "\t\t\t\t" . $city . "\t" . $state . "\t" . $zip . "\t" . $zip4 . "\t" . $studentbody . "\t" . $phone . "\t" . $extension . "\t" . $fax . "\t" . $email . "\t" . $web . "\t" . $autocoop . "\t" . $othercoop . "\t" . $libdirfirst . "\t" . $libdirlast . "\t" . $libdirphone . "\t" . $libdirext . "\t" . $libdiremail . "\t" . $dirjobtitle . "\t" . $alamlsfte . "\t" . $otherlibfte . "\t" . $totlibfte . "\t" . $totopexp . "\t" . $totvolumes . "\t" . $mondayopen . "\t" . $mondayclosed . "\t" . $tuesopen . "\t" . $tuesclosed . "\t" . $wedopen . "\t" . $wedclosed . "\t" . $thursopen . "\t" . $thursclosed . "\t" . $friopen . "\t" . $friclosed . "\t" . $satopen . "\t" . $satclosed . "\t" . $sunopen . "\t" . $sunclosed . "\t" . $illtoown . "\t" . $illtoinspec . "\t" . $illtoinstother . "\t" . $illtoout . "\t" . $illtototal . "\t" . $illfromown . "\t" . $illfrominspec . "\t" . $illfrominother . "\t" . $illfromout . "\t" . $illfromtotother . "\t\t\t\t\t\t\t\t;

      $filename = '/tmp/acad.txt';

      $fp = fopen($filename, "w");

      fwrite($fp,$writetofile);

      fclose($fp);

      // Load data from file to table

      $mysqlcommand = '"load data infile \'/tmp/acad.txt\' into table libdev.alllibdir"';

      $syscommand = "echo $mysqlcommand | mysql --user=$mysql_user --password=$mysql_password";

      system($syscommand, $retval);

      // execute SQL query and get result

      $result=mysql_query($query) or die ("Couldn't execute query");

      if (!$result) {

      echo "<P>Couldn't add record!";

      } else {

      echo "You're record has been added";

      // finished with the echo statement

      }

      ?>

      #2; Fri, 16 Nov 2007 10:11:00 GMT
    • Forgot the quote at the end of \t\t\t\t\t

      You must have errors turned off.

      #3; Fri, 16 Nov 2007 10:12:00 GMT
    • Yikes...

      You could probably lose a few hundred lines of code by setting variables to post variables if you just used extract($_POST);

      #4; Fri, 16 Nov 2007 10:13:00 GMT
    • More importantly, reread the section of the manual that explains how variables embedded in double-quoted strings are handled. It will help you avoid writing code that you can't read and can't debug.

      All of that string concatenation is just not necessary.

      #5; Fri, 16 Nov 2007 10:14:00 GMT