PHP - Equivalent Double Pipe Syntax To Check If Variable Already Initialized In Php?
Hey all,
Let's say I want to do something like this: Code: [Select] <?php function exists($a){ $b ||= $a; } echo exists(1); ?> Basically, every time exists is called, it will only assign b to a if b isn't already initialized. What's the most effective way to achieve the equivalent in php? ternary? Thanks for response. Similar TutorialsI have been looking at this code most of the morning and do not have a clue what is wrong with the code. I am hoping its not a stupid mistake, can someone please help me out? thank you
<title>Inputing Travel Detials</title> <header> <h1 align="center"> Adding Travel Detials </h1> <body> <p> <center><img src="cyberwarfareimage1.png" alt="Squadron logo" style="width:200px;height:200px" style="middle"></center> <table border="1"> <tr> <td><a href="index.php"> Home Page </a></td> <td><a href="administratorhomepage.html">Administrator Home Page </a></td> <td><a href="viewhomepage.html">View Home Page </a></td> <td><a href="Inputhomepage.html">Input Home Page </a></td> <td><a href="traveldetials.html">Enter More Travel Detials </a></td> </table> </p> <?php include "connection.php"; $Applicant_ID = $_POST["Applicant_ID"]; $Method_Of_Travel = $_POST["Method_Of_Travel"]; $Cost = $_POST["Cost"]; $ETA = $_POST["ETA"]; $Main_Gate_Advised = $_POST["Main_Gate_Advised"]; $query = ("UPDATE `int_board_applicant` SET `Method_Of_Travel`=`$Method_Of_Travel', `Cost`=`$Cost', `ETA`='$ETA', `Main_Gate_Advised`='$Main_Gate_Advised' WHERE `Applicant_ID`='$Applicant_ID'"); $result = mysqli_query($dbhandle, $query) or die(mysqli_error($dbhandle)); if($result){ echo "Success!"; } else{ echo "Error."; } // successfully insert data into database, displays message "Successful". if($query){ echo "Successful"; } else { echo "Data not Submitted"; } //closing the connection mysqli_close($dbhandle) ?> Ok this is puzzleing. I am geting "Could not delete data: 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 '1' at line 1". but its is deleting the entry that needs to be removed. The "1" is the entry. Just not sure what is causing the error. I do have another delete php but I have put that on the back burning for the time being.
<?php $con = mysqli_connect("localhost","user","password","part_inventory"); // Check connection if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } else { $result = mysqli_query($con, "SELECT * FROM amp20 "); $amp20ptid = $_POST['amp20ptid']; // escape variables for security $amp20ptid = mysqli_real_escape_string($con, $_POST['amp20ptid']); mysqli_query($con, "DELETE FROM amp20 WHERE amp20ptid = '$amp20ptid'"); if (!mysqli_query($con, $amp20ptid)); { die('Could not delete data: ' . mysqli_error($con)); } echo "Part has been deleted to the database!!!\n"; mysqli_close($con); } ?> Hi guys
I have this code below and all works fine when submitting this online application apart from when someone types either ' # & into one of the comment fields in which it throws up the error. Have tried various fixes from across the internet but no joy. Can anyone offer suggestions?
<?php
$con = mysql_connect("localhost:3306","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('sfapp', $con);
$sql="INSERT INTO 'sfapp' ('surname_add','forename_add','dob_add','hometele_add','mobiletele_add','homeadd_add','siblings_add','schoolname_add','headname_add','schooladd_add','schooltele_add','schoolem_add','alevel_add','personstate_add','nameprovided_add','pe_add','se_add','PredGrade_Art','PredGrade_AScience','PredGrade_BusStudies','PredGrade_Electronics','PredGrade_EnglishLang','PredGrade_EnglishLit','PredGrade_French','PredGrade_German','PredGrade_Geog','PredGrade_Graphics','PredGrade_History','PredGrade_Maths','PredGrade_SepScience','PredGrade_ProductDesign','PredGrade_Spanish','PredGrade_Other','Gender_Male','Gender_Female','Sub_EnglishLit','Sub_Maths','Sub_FurtherMaths','Sub_Biology','Sub_Chemistry','Sub_Physics','Sub_French','Sub_German','Sub_Spanish','Sub_Geography','Sub_History','Sub_RE','Sub_FineArt','Sub_Business','Sub_Computing','Sub_GlobPersp','Sub_DramaAndTheatre','Sub_PE','Sub_Dance','Sub_Politics','Sub_Psychology','Sub_Sociology','readprospect_chk','Sib_Yes','Sib_No','Current_Student_Yes','Current_Student_No','I_Understand_chk','Current_Education_chk','Local_Care_chk','Staff_Cwhls_chk','Sub_Film')
VALUES
('$_POST[surname_add]','$_POST[forename_add]','$_POST[dob_add]','$_POST[hometele_add]','$_POST[mobiletele_add]','$_POST[homeadd_add]','$_POST[siblings_add]','$_POST[schoolname_add]','$_POST[headname_add]','$_POST[schooladd_add]','$_POST[schooltele_add]','$_POST[schoolem_add]','$_POST[alevel_add]','$_POST[personstate_add]','$_POST[nameprovided_add]','$_POST[pe_add]','$_POST[se_add]','$_POST[PredGrade_Art]','$_POST[PredGrade_AScience]','$_POST[PredGrade_BusStudies]','$_POST[PredGrade_Electronics]','$_POST[PredGrade_EnglishLang]','$_POST[PredGrade_EnglishLit]','$_POST[PredGrade_French]','$_POST[PredGrade_German]','$_POST[PredGrade_Geog]','$_POST[PredGrade_Graphics]','$_POST[PredGrade_History]','$_POST[PredGrade_Maths]','$_POST[PredGrade_SepScience]','$_POST[PredGrade_ProductDesign]','$_POST[PredGrade_Spanish]','$_POST[PredGrade_Other]','$_POST[Gender_Male]','$_POST[Gender_Female]','$_POST[Sub_EnglishLit]','$_POST[Sub_Maths]','$_POST[Sub_FurtherMaths]','$_POST[Sub_Biology]','$_POST[Sub_Chemistry]','$_POST[Sub_Physics]','$_POST[Sub_French]','$_POST[Sub_German]','$_POST[Sub_Spanish]','$_POST[Sub_Geography]','$_POST[Sub_History]','$_POST[Sub_RE]','$_POST[Sub_FineArt]','$_POST[Sub_Business]','$_POST[Sub_Computing]','$_POST[Sub_GlobPersp]','$_POST[Sub_DramaAndTheatre]','$_POST[Sub_PE]','$_POST[Sub_Dance]','$_POST[Sub_Politics]','$_POST[Sub_Psychology]','$_POST[Sub_Sociology]','$_POST[readprospect_chk]','$_POST[Sib_Yes]','$_POST[Sib_No]','$_POST[Current_Student_Yes]','$_POST[Current_Student_No]','$_POST[I_Understand_chk]','$_POST[Current_Education_chk]','$_POST[Local_Care_chk]','$_POST[Staff_Cwhls_chk]','$_POST[Sub_Film]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
?>
<?php
//if "email" variable is filled out, send email
if (isset($_REQUEST['pe_add'])) {
//Email information
$admin_email = $_REQUEST['pe_add'];
$forename = $_REQUEST['forename_add'];
$email = "autoreply@testing.com";
$subject = "Application";
$desc =
"Dear $forename
Thank you for submitting your online application, we will be in touch shortly.
"
;
//send email
mail($admin_email, "$subject", "$desc", "From:" . $email);
//Email response
echo "Thank you for contacting us!";
}
//if "email" variable is not filled out, display the form
else {
?>
If you are seeing this, you need to go back and fill out the Personal Email section!
<?php
}
header("location:complete.php");
mysql_close($con)
?>
Thanks in advance.
Hi all, I am new to PHP and wish to use it simply to collect form data and email it to myself. From what I have gathered from PHP tutorials I have write the following script but I am getting syntax error warnings and cant figure out why. Can somebody please tell me where I am going wrong and if this script will actually work? The line I am getting errors on is Code: [Select] <?php //message contains $message = "Name:$name</br>Email:$email</br>Phone:$phone</br>Preferred Date:$date</br>Best Time To Contact:$time"; ?> Thank you in advance. Code: [Select] <body> <?php //my email $recipient = "info@hairlosslasergroup.co.uk"; //persons name $name = htmlspecialchars($_POST['name']); //persons email $email = htmlspecialchars($_POST['email']); if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { die("E-mail address not valid"); } //persons phone number $phone = htmlspecialchars($_POST['phone']); //preferred date $date = htmlspecialchars($_POST['comments']); //best time $time = htmlspecialchars($_POST['time']); //check required fields $required = check_input($_POST['required'], "Please go back and complete all required fields"); //email subject $subject = ($_POST['subject'] //message contains $message = "Name:$name</br>Email:$email</br>Phone:$phone</br>Preferred Date:$date</br>Best Time To Contact:$time"; //message mail($recipient, $subject, $message); //redirect to thank you page header('Location:$redirect'); ?> </body> </html> Hello I have the following error: 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 players WHERE ((NOT inactive_player) AND ((players.Player_Last_Name = 'p' at line 3 I think my error is in the statement below... Code: [Select] echo "1"; $getNewPlayers = "SELECT players.Player_number, players.Player_name, FROM players WHERE ((NOT inactive_player) AND ((players.Player_Last_Name = 'player_find%'))) ORDER BY player_name"; $rsNewPlayers = mysql_query($getNewPlayers, $link) or die (mysql_error()); $varNewCount = mysql_num_rows($rsNewPlayers); echo $varNewCount['Player_name']; Can you tell me where the error is and how I might go about to fix it? Thanks, Hi, I joined new here, and I am really having hard time figuring out what's wrong with the code below which isn't working. if (isset($_POST['privacy_submit'])) { if (($_SERVER['REQUEST_METHOD'] == "POST")) { $privacy_upd = mysql_query("UPDATE clf_privacy_terms SET 'p_bannerads' = '".$_POST['p_bannerads']."', 'p_shareinfo' = '".$_POST['p_shareinfo']."', 'p_crossmarketing' = '".$_POST['p_crossmarketing']."', 'p_tacking' = '".$_POST['p_tacking']."', 'p_sendcommunication' = '".$_POST['p_sendcommunication']."', 'p_under13' = '".$_POST['p_under13']."', 'p_internationally' = '".$_POST['p_internationally']."', 'p_discloselegal' = '".$_POST['p_discloselegal']."', 'p_server_country' = '".$_POST['p_server_country']."', 'p_forums' = '".$_POST['p_forums']."', 'p_newslettermodule' = '".$_POST['p_newslettermodule']."', 'p_membershipmodule' = '".$_POST['p_membershipmodule']."' ") or die(mysql_error()); $msg="Successfully Updated!"; } } else { $msg="<font color=\"red\">Incorrect method of submission.</font> Please try again."; } ?> <h2>Privacy Policy</h2> <?php if ($privacy_upd) { echo ($msg); } ?> <form class="box" name="privacy_upd" action="gen.php" method="post"> ......................................................................................... ........................ MySQL Error returns the following: Code: [Select] 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 ''p_bannerads' = 'do not', 'p_shareinfo' = 'do not', 'p_cross' at line 2 Let me know if any of your guys can solve it for me, I would appreciate since I am not able to solve it for last two days already. Thank you... I've been doing some benchmarking for memory usage for a very resource-sensitive project I'm doing (that I probably shouldn't be using PHP for, but oh well). I came across something peculiar, and I'm trying to figure out why I get results as I do. Here is my test file: <?php $string_parts = 4; $max = 10000; $arr = array(); $i = 0; $start = memory_get_usage(); for(;$i<$max;$i++){ $arr[] = array_pad(array(), 4, "A"); } $end = memory_get_usage(); printf("%d iterations, %d bytes in array\n", $max, $end-$start); $arr2 = array(); $i = 0; $start2 = memory_get_usage(); for(;$i<$max;$i++){ $arr2[] = array("A", "A", "A", "A"); } $end2 = memory_get_usage(); printf("%d iterations, %d bytes in array (%.2f%%)\n", $max, $end2-$start2, ($end2-$start2)/($end-$start)*100); ?> Output on my system: Code: [Select] 10000 iterations, 4145696 bytes in array 10000 iterations, 5585744 bytes in array (134.74%) A 35% memory gain for two of the same exact arrays seems really weird. Any information as to why, or perhaps any useful functions or resources would be appreciated. Hi, I have been trying to make a page which shows a number of thumbnails, populated by images and text from a database, this I got working perfectly fine, but as I am now trying to make it so users can log in and edit or delete each one, it has all started to go a bit wrong and I was wondering if anyone could spot why?!? It is currently bringing up this message on the screen: Error fetching entries from the database, error: Statement: SELECT image,id,projecttitle,projectcode,FROM portfolio ORDER BY id DESC LIMIT 16 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 portfolio ORDER BY id DESC LIMIT 16' at line 1 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /Users/mdfcows/Sites/atelier/portfolio.php on line 129 I don't quite know how to get it to print out the full nature of the error, but this is the code I am using: Code: [Select] <?php $max_items2 = 16; /* max number of news items to show */ require_once('config.php'); $db2 = mysql_connect (DB_HOST,DB_USER,DB_PASSWORD); if(!$db2) { die('Failed to connect to server: ' . mysql_error()); } mysql_select_db (DB_DATABASE,$db2); function displayNews($all2 = 0) { global $db2, $max_items2; if ($all2 == 0) { /* query for news up to $max_items */ $query9 = "SELECT image,id,projecttitle,projectcode," . "FROM portfolio ORDER BY id DESC LIMIT $max_items2"; } else { /* query for all news */ $query9 = "SELECT image,id,projecttitle,projectcode," . "FROM portfolio ORDER BY id DESC"; } $result9 = mysql_query($query9) or print("<p>Error fetching entries from the database, error: " . "Statement: " . $query9 . "</p>" . mysql_error()); while ($row9 = mysql_fetch_assoc ($result9)) { $title = htmlentities ($row9['projecttitle']); $news = nl2br (strip_tags ($row9['projectcode'], '<a><b><i><u>')); $image = $row9['image']; $id = $row9['id']; /* display the data */ ?> <div class="boxgrid captionfull"> <a id="<?php echo $id ?>" <a class='visit' href="project.php?id=<?php echo $news ?>"> <?php echo $image; if(strstr($image, 'jpg') or (strstr($image, 'png')) or (strstr($image, 'gif')) or (strstr($image, 'bmp')) or (strstr($image, 'jpeg')) ) { echo "<img class='cpimg' src='images/portfolio/$image' alt='' />"; }else { echo "";} ?>"></a> <div class="cover boxcaption" style="top: 122px; "> <p align="left"><?php echo $title ?></p> </div> </div> <?php //Check whether the session variable SESS_MEMBER_ID is present or not if(isset($_SESSION['SESS_MEMBER_ID']) || (!trim($_SESSION['SESS_MEMBER_ID']) == '')) { echo "<form class='editbtn' action='editportfolio.php' method='POST'>"; echo "<input type='hidden' name='idf' value='$id' />"; echo "<input src='images/editbtn.png' type='image' value='Edit' />"; echo "</form>"; echo "<form class='editbtn' action='deletportfolio.php' method='POST'>"; echo "<input type='hidden' name='ide' value='$id' />"; echo "<input src='images/delbtn.png' type='image' value='Delete' />"; echo "</form>"; } else { echo ""; } } } /* this is where the script decides what do do */ echo "\n"; switch($_GET['action']) { case 'all': displayNews(1); break; default: displayNews(); } echo "\n"; ?> <?php //Check whether the session variable SESS_MEMBER_ID is present or not if(isset($_SESSION['SESS_MEMBER_ID']) || (!trim($_SESSION['SESS_MEMBER_ID']) == '')) { echo "<span class='show_all'>"; echo "<a class='show' href='admincp.php'> Admin</a>"; echo "</span>"; echo "<span class='show_all'>"; echo "<a class='show' href='logout.php'> Logout</a>"; echo "</span>"; } else { echo ""; } ?> with the line, " while ($row9 = mysql_fetch_assoc ($result9)) {" being line 129 any help would be much appreciated! Thank you, Martin $pastelink = "<br /><a href='view.php?paste=$lol&language=$language'>$name</a>"; mysql_query("INSERT INTO recent (url) VALUES ('$pastelink')"); That query won't run, however if I do this: $test = $_POST['name']; //$pastelink = "<br /><a href='view.php?paste=$lol&language=$language'>$name</a>"; mysql_query("INSERT INTO recent (url) VALUES ('$test')"); It will run, is this because of the single quotes in $pastelink? & If so, how can I fix it? Just as the title says. In cPanel, I have it al configured just fine; the script recieves the email, and I do receieve an email back, but here's where I come into problems. It simply will NOT save the attachments to the server, I've tried loads of different implementations I've found, and nothing's working. pipe script: #!/usr/local/bin/php <?php // Need PEAR installed include('Mail.php'); include('Mail/mime.php'); require_once 'Mail/mimeDecode.php'; // read email using stdin $fx = fopen("php://stdin", "r"); $email = ""; while (!feof($fx)) { $email .= fread($fx,1024); } $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $params['input'] = $email; /* $message=new Mail_mimeDecode(); $mailObj=$message->decode($params); */ $mailObj = Mail_mimeDecode::decode($params); // Who is it from $from=$mailObj->headers['from']; // Get Subject $subj=$mailObj->headers['subject']; // Get Message Body $body=$mailObj->parts[0]->body; $gather="From:$from\nSubject:$subj\nBody:$body"; // Get and Save the Attachments foreach($mailObj->parts as $pp){ if ($pp->disposition=='attachment'){ $tmp=$pp->d_parameters['filename']; if($tmp !== ""){ $open = fopen($tmp, 'w'); if($open){ $write = fwrite($open,$pp->body); if(!$write){ $err = "Cannot write to file: '".$tmp."' - var: ".$write; } }else{ $err = "Cannot create/open file: '".$tmp."' - var: ".$open; } } } } $tox = $from; $subjectx = 'Your Photo was Uploaded!'; $messagex = $err; $headersx = 'From: piculo.us Uploader <upload@piculo.us>' . "\r\n" . 'Reply-To: upload@piculo.us' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($tox, $subjectx, $messagex, $headersx); exit; ?> Here's the email I get back: Cannot create/open file: '2879_1150712806251_1180540351_425140_7940412_n.jpg' - var: as you can see, it is aware that an attachment exists, but it won't create the file, and fopen() doesn't say anything. Any ideas?? What's the correct syntax for having a variable in a select statement? Here's an example of what I'm trying to do (after I'm already connected to the database). Code: [Select] $username = "thomas"; $query = mysql_query( "SELECT * from users WHERE username = $thomas" ); if( mysql_num_rows( $query ) > 0 ) { bla bla... It works if I don't put the WHERE part in, but I get an error if I use it, so I'm assuming I have the wrong syntax for using a variable in the select statement. Have a ton of pop-up javascript links in my page. To tidy up the code, I have decided to declare all links at top of the page to keep them together and simply use the variable within the "a" brackets. Let me show you: Code: [Select] <? $brand_edit = 'javascript:void(0)"onclick="window.open('/admin/form.php?db=outlet&tbl=brands&auto_increment=<? echo $auto_increment; ?>&action=edtamp;step=1','none','width=750,height=250,menubar=no,status=no,resizable=no,location=no,toolbar=no,scrollbars=yes,left=50,top=50,titlebar=no')'; ?> <span class="float" style="width: 100px;"><a href="<? echo $brand_edit; ?>"><? echo $auto_increment; ?></a></span> However, something things to go wrong as I'm getting an "Parse error: syntax error, unexpected '=' in ... on line 12" Where am I going wrong with my syntax? I want an if statement that not only checks for the existence of a variable, but which also checks all the variables already created for one with a specific value. For example, I have a loop which creates variables, but I don't want it to make two variables with the same value. The problem is, using
if(isset(${'h'.$x}==false) && ${'h'.$x} != 3{ }won't work since the variable equal to 3 could be named h4, whereas this one will be named h5. I hoped I explained this efficiently enough, sorry about any confusion. Hi , I have a login page that i acces via www.mydomain.com/logib.php?last_visited_page.php however the passed in variable last_visited_page.com is not always set . once the login script has run , when the variable it is not set i would want jump to myaccount.php otherwise jump to last_visited_page.php How do i check for no variable beinf present and therefore jump to myaccount.php Many Thanks Fraser I have a query: $query ="select * FROM telephone_admin where name Like ' ".$_GET['answer']." ' "; I would like to add % before and after so that the query will search for result which contains the PHP variable $_GET['answer'] before or after, but the problem is how can I add % before or after ' ".$_GET['answer']." ' within the query?? Thanks! I was wondering if it is possible to check the content for a variable for a certain work. For example say a variable called "$variable1" was equal to "one, two, three, four, five". How can I check to see if the variable contain the word "four"? Thanks for any help. i am pulling the login date from the databases and i want it to check if the variable is empty and if it is echo Never Loged In and if it not empty echo the date <?php include'db.php'; $id=$_GET['id']; $sql=mysql_query("SELECT * FROM admin WHERE id='$id'")or die(mysql_error()); while($row=mysql_fetch_array($sql)) { $date=$row['login_date']; if($date > 0) { echo"Never Loged In"; } else { echo"$date"; } } ?> if ($d = opendir("uploads")) { while ($file = readdir($d)) { if ($file != "." && $file != ".." ) // if( is_file($file) ) { $files[] = $file; } } closedir($d); }if ($file != "." && $file != ".." $file != ???????? ) just a bit stuck here |