-
Posts
3,404 -
Joined
-
Last visited
-
Days Won
55
Everything posted by Ch0cu3r
-
As you are only looping once, you do not need to the for loop. So remove the for loop and replace $pic[$i] with $pic[0] Now you can save $pic[0] (which will be the filename of the image) to the database
-
I save the users details in the session for use latter on. The data then becomes persistent whilst the user is logged in, provided you call session_start on all pages you use sessions. You then don't need to keep querying the database to get their info.
-
Need to translate datetime from 10 digit to standard date
Ch0cu3r replied to systemsgotyou's topic in PHP Coding Help
Not sure why you'd want to save the formatted date. Raw unformatted data should be stored, you'd format the data to your chosing when you go to display it. If you want to save the formatted date to all records then I guess you can use an update query UPDATE table SET formatted_date_field = DATE_FORMAT(FROM_UNIXTIME(non_formated_date_field), '%d/%m/%Y'); -
How are you chosing the item from the array? Just save the item you chosen to the database example $pics = array('a.pjg', 'b.jpg', 'c.jpg'); // shuffle pics order shuffle($pics); // get the first item from the shuffle array $pic = $pics[0]; // save $pic to database // display the chosen pic echo '<img src="'.$pic.'" />';
-
Have you tried googling "php shout box". The first link for me gives a tutorial and the second link provides a php script.
-
Why are you allow users to login from two different locations? You should provide login from one location. You then decide where to redirect the user based on their access level, eg admin users go to admin cp, and everyone else goes to your homepage. In order to determine their access level you need to store that in your database. Example code // query the database and get the users data, when username and password match $query = "SELECT username, email, access_level FROM users WHERE username='$username' AND password='$password'"; $result = mysql_query($query); if($result) { // did query return any rows? if(mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); // save user data to session $_SESSION['username'] = $row['username']; $_SESSION['email'] = $row['email']; $_SESSION['access_level'] = $row['access_level']; // redirect based on access level if($row['access_level'] == 'admin') { header('location: /college/admin/'); // for admins } else { header('location: /college/'); // for everyone else } exit; } else { echo 'login failed, username password did not match'; } } else { // query failed probably due to an error }
-
Could you tell us what it is you are looking for in the page?
-
If all .html files are in the same folder you can use glob to loop over them and then extract the line you are after from them. $itemtype = array(); // loop over all .html files foreach(glob('*.html') as $file) { // find and extract the value from itemtype variable if(preg_match('~var itemtype = "([^"]+)";~', file_get_contents($file), $matches)) { // add value to itemtype array. $itemtype[] = $matches[1]; } } printf('<pre>%s</pre>', print_r($itemtype, true));
-
Leading zero's infront of numbers are ignored. Wrap your month numbers within quotes
-
If you know the positions then use substr $DL = ';6360101262000786187=2105197499270=?'; $numbers = array(); // number positions $postions = array(15, 23, 31); // loop over the positons foreach($postions as $postion) { // get the number $numbers[] = substr($DL, $postion, 2); } printf('<pre>%s</pre>', print_r($numbers, true));
-
Phpinfo not running in webpage
Ch0cu3r replied to ksbigfoot's topic in PHP Installation and Configuration
Something is causing the 500 internal server error. The acutall error message should be logged in the IIS servers error log. -
You most probably are querying the database and echo'ing the data twice. Without seeing the code for revDisp5.php we cannot specifically tell you why.
-
echo $row; should be echo $row['emp_name']; test1 needs to be wrapped in quotes on line 14 mysql_select_db('test1', $con)
-
Why my search engine Goes wrong Everytime I have User Logged In
Ch0cu3r replied to ava5193's topic in PHP Coding Help
Your code is generating the mysql query, for some reason it is causing an error in the query. Echo your query to see if its formatted correctly, // when mysql returns an error, mysql_query will return false if(!$query) { // output the error from query trigger_error('Query has failed: ' . mysql_error()); echo "Query: <pre>$query</pre>"; } -
in that case in revDisp4.php you need to get the newest record (which will be the last) SELECT * FROM testTable1 ORDER BY id DESC LIMIT 1 I assume you have an id column. or when you insert the record, get the records id and pass that to your script $sql="INSERT INTO testTable1 (emp_name, emp_address) VALUES ('$_POST[emp_name]','$_POST[emp_address]')"; if (!mysql_query($sql,$con)) { die('Error:' . mysql_error()); } $record_id = mysql_insert_id(); // get the id of the record that was inserted // pass the records id to the redirect page $redirect_page = 'http://redlinedown.com/revDisp4.php?id='.$record_id; Now in revDisp4.php you'd get the records id from $_GET['id']. example <?php session_start(); ini_set('display_errors', 'On'); error_reporting(E_ALL | E_STRICT); $con = mysql_connect("localhost","admin****","*Bridlepath*","test*"); if (!$con) { die('NEIN!' . mysql_error()); } //Connect to DB //(Possible offender below) mysql_select_db(test1, $con) or die("Lost"); // does the id exist? if(isset($_GET['id'])) { // get the record id $record_id = intval($_GET['id']); echo "<table border='1'> <tr> <th>Employee Name</th> <th>Employee Address</th> </tr>"; // fetch the record that matches the id $result = mysql_query("SELECT * FROM testTable1 WHERE id = $record_id") or die (mysql_error()); $row = mysql_fetch_array($result) echo '<table><tr>'; echo '<td>'; echo '<Strong>Name:</strong><br/>'; echo $row; echo '</td>'; echo '</tr></table>'; } mysql_close($con); ?>
-
get row within class, or pass row to class?
Ch0cu3r replied to RuleBritannia's topic in PHP Coding Help
What is it you are trying to do? What row? What class? -
What code are you using to redirect? What is wrong with it? What should it be doing?
-
You cannot mix javascript and PHP code together. Is data the response from an ajax request? if it is, then call the showBBcodes PHP function when you receive the ajax request in PHP.
-
Help with the coding for contact form with attachment
Ch0cu3r replied to spanner90's topic in PHP Coding Help
First of all the form field names do not match to the ones you are trying to retrieve from $_POST $name = $_POST['name']; // <-- should be $_POST['fieldFormName'] $email = $_POST['email']; // <-- should be $_POST['fieldFormEmail'] $phone = $_POST['phone']; // <-- should be $_POST['fieldSubject'] $covering_letter = $_POST['covering_letter']; // <-- should be $_POST['fieldDescription'] Because you do not have any code to retrieve the uploaded file. Have a read of how PHP handles file uploads For the uploaded file to be attached to the email you need to set the appropriate attachment headers. This tutorial gives an example of how to do thise -
User input validation eith regular expressions
Ch0cu3r replied to terungwa's topic in PHP Coding Help
Because you are inserting the record before you verify the users input. You need to first verify the input, and then insert the record if it passes your requirements $_POST["last_name"] and $_POST["first_name"] wont exist until the form is submitted. You are not checking to see if they exist before you used preg_match. I have rearranged your code, so the user input is verified first, and a record is inserted when the input pass your requirements <?php $errors = array(); // .....create database connection......// if (isset($_POST['insert'])) { $last_name = trim($_POST['last_name']); $first_name = trim($_POST['first_name']); // verify users input // check last_name field if(!preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["last_name"])) { // set error for last_name field $errors['last_name'] = '<p class="errText">Last Name must be from letters, dashes, spaces and must not start with dash</p>'; } // check first_name field if(!preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["first_name"])) { // set error for first_name field $errors['first_name'] = '<p class="errText">First Name must be from letters, dashes, spaces and must not start with dash</p>'; } // if no errors are set, inset record into database if(empty($errors)) { // initialize flag $OK = false; // initialize prepared statement $stmt = $conn->stmt_init(); // create SQL $sql = 'INSERT INTO voter_tracking ( v_id, last_name, first_name) VALUES(?, ?)'; if ($stmt->prepare($sql)) { // bind parameters and execute statement $stmt->bind_param('iss', $_POST['v_id'], $_POST['last_name'], $_POST['first_name']); // execute and get number of affected rows $stmt->execute(); if ($stmt->affected_rows > 0) $OK = true; } // redirect if successful or display error if ($OK) { echo 'posted'; exit; } else $error = $stmt->error; } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Home</title> </head> <body> <div id="main"> <fieldset> <legend><h2>Add New Voter Record:</h2></legend> <?php if (isset($error)) { echo "<p class=\"warning\">Error: $error</p>"; } ?> <form id="form1" method="post" action=""> <p> <label for="last_name">Last Name:</label> <?php // ouput error for last_name field if(isset($errors['last_name'])) echo $error['last_name']; ?> <input type="text" name="last_name" class="widebox" id="name" required aria-required="true"> </p> <p> <label for="first_name">First Name:</label> <?php // ouput error for first_name field if(isset($errors['first_name'])) echo $error['first_name']; ?> <input type="text" name="first_name" class="widebox" required aria-required="true"> </p> <p> <input type="submit" name="insert" value="Insert New Entry" id="insert"> </p> </form> </fieldset> </div> </body> </html> -
I still recommend mysqli <?php $host = 'localhost'; $user = ''; $db = ''; $password = ''; // Create connection $con=mysqli_connect($host,$user,$password,$db); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } else echo "connected"; ?> <?php $result= mysqli_query($con, "SELECT * FROM jos_content"); while($row = mysqli_fetch_assoc($result)){ echo $row['title']; } ?>
-
Trim, explode and floor $string = '(215.354100, 218.613800, 2501.263000)'; // trim the () from the start/end of string and explode on comma and space $data = explode(', ', trim($string, '()')); // apply floor to round number down to nearest whole integer (removes the decimal) $data = array_map('floor', $data); // create the new string $new_string = sprintf('(%s)', implode(', ', $data)); echo "$string<br />$new_string"; Or a simple preg_replace with regex $new_string = preg_replace('~(\.\d+)~', '', $string);
-
You cannot use mysqlI_* (mysql improved) functions and mysql_* (original) functions together. You can only use one or the other, personally I'd use mysqli as the original library is deprecated and could be removed from future versions of PHP.