Jump to content

Balderick

Members
  • Posts

    75
  • Joined

  • Last visited

Everything posted by Balderick

  1. I tried that one, but the problem is that I get an error message. the error is located in this line: else if ( !$stmt->bind_param('i', $id ) ) { I also tried to put in all columns but allegedly that's not the solution either. can you tell what is required to fetch all the data found in the table?
  2. Desperately needed: a way to retrieve all records from a database table while using prepared statements. The script now works but only for 1 record. <?php $_POST['id']= '2'; // set connection variables // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $stmt = $conn->prepare("SELECT * FROM table WHERE id LIKE ? ;"); if ( !$stmt ) { yourErrorHandler($conn->error); // or $mysqli->error_list } else if ( !$stmt->bind_param('i', $id ) ) { yourErrorHandler($stmt->error); // or $stmt->error_list } else if ( !$stmt->execute() ) { yourErrorHandler($stmt->error); // or $stmt->error_list } else { $result = $stmt->get_result(); while ($row = mysqli_fetch_assoc($result)) { var_dump($row); $data[]=$row; var_dump($id); var_dump($data); $array= implode("|" , $row) ; var_dump($array); foreach( $data as $row ) { $id = $row['id']; $name = $row['name']; var_dump($id); var_dump($name); } } } ?> I hope someone can give a query that executes what is desired, but maybe there is also a possibilty to put the query in a loop? Please help.
  3. I'm trying to set chroot. I get the following error message: ( ! ) Fatal error: Call to undefined function chroot() in X:\wamp\www\dir1\dir2\test-getcwd.php on line 5 Call Stack # Time Memory Function Location 1 0.0005 130720 {main}( ) ...\test-getcwd.php:0 my script is like this: <?php echo getcwd() . "\n"; chroot('X:\wamp\www\dir3') . "\n"; echo getcwd() . "\n"; ?> Is it possible chroot is not default set in wamp or am I making a mistake?
  4. I have one page in one directory. The subdomain is exploded from the http address and fetches the required data. I think it's unnecessary extended to make multiple directories. Is it the only solution?
  5. I'm looking for directives that help solving the problem of redirecting a not existing subdomain towards the www subdomain. I know/guess .htaccess is used, but cant figure out how. My present situation is that existing subdomains are redirected well. I adjusted virtual hosts in this way: <VirtualHost *:80> ServerName mysite.dev ServerAlias mysite.dev *.mysite.dev DocumentRoot X:/wamp/www/TestVirtHost/ <Directory "X:/wamp/www/TestVirtHost/"> Options +Indexes +FollowSymLinks +MultiViews AllowOverride All Require local </Directory> </VirtualHost> my etc/hosts file has the following rules 127.0.0.1 *.mysite.dev mysite.dev 127.0.0.1 subdomain_one.mysite.dev 127.0.0.1 subdomain_two.mysite.dev Can some expert explain please what the .htaccess should look like for the problem explained above.
  6. I finally fixed it, but I used global. Is there a difference in using global and using an array as output? In general, is it bad to use global to augment the scope of a variable output or only in specific circumstances? Solution: <?php function count_all_days($input){ global $input; global $out; $pres = new DateTime(); $date_set = date_create($input); $pres->format('Y-m-d'); $countday = date_diff($date_set, $pres); $out = $countday->format("%R%a days"); $out = $out+2; // the first and last day are not counted return $out; } ?> The output, which is not an array, is suitable for usage inside my html lay out.
  7. I cant find any solution, but I made a more simplified set-up for the function. Not yet with a loop, but the output is wrong in the same way. <?php function GetDays($sStartDate, $sEndDate){ // Firstly, format the provided dates. // This function works best with YYYY-MM-DD // but other date formats will work thanks // to strtotime(). $sStartDate = gmdate("Y-m-d", strtotime($sStartDate)); $sEndDate = gmdate("Y-m-d", strtotime($sEndDate)); // Start the variable off with the start date $aDays[] = $sStartDate; // Set a 'temp' variable, sCurrentDate, with // the start date - before beginning the loop $sCurrentDate = $sStartDate; // While the current date is less than the end date while($sCurrentDate < $sEndDate){ // Add a day to the current date $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate))); // Add this new day to the aDays array global $aDays; // deze global heb ik zelf toegevoegd en MOET hier staan, iig niet buiten deze conditie global $tot_days; $aDays[] = $sCurrentDate; } // Once the loop has finished, return the // array of days. $tot_days = count($aDays); //return $aDays; $tot_days = $tot_days +1; return $tot_days; } $sStartDate =''; $sStartDate = '2016-10-1'; $sEndDate =date("Y-m-d"); GetDays ($sStartDate, $sEndDate); var_dump($tot_days); echo '<br> ' . $tot_days . ' is the number of days that ' . $sStartDate . ' and ' . $sEndDate . ' are separated ! ' ; $sStartDate =''; $sStartDate = '2016-9-1'; $sEndDate =date("Y-m-d"); GetDays ($sStartDate, $sEndDate); var_dump($tot_days); echo '<br> ' . $tot_days . ' is the number of days that ' . $sStartDate . ' and ' . $sEndDate . ' are separated ! ' ; $sStartDate =''; $sStartDate = '2016-9-24'; var_dump($sStartDate); $sEndDate =date("Y-m-d"); GetDays ($sStartDate, $sEndDate); var_dump($tot_days); echo '<br> ' . $tot_days . ' is the number of days that ' . $sStartDate . ' and ' . $sEndDate . ' are separated ! ' ; ?> Though, the difference between output 3 and 2 is eaxctly the number of days I'm looking for. is there another, better and faster way to solve this?
  8. I found an online function called GetDays and I slightly modified it to calculate the number of days between a past date and the current time. The function works fine for 1 input, but I have a table full of records and want to use the function to get the total days for each record separatedly. <?php // function GetDays($sStartDate, $sEndDate){ // Firstly, format the provided dates. // This function works best with YYYY-MM-DD // but other date formats will work thanks // to strtotime(). $sStartDate = gmdate("Y-m-d", strtotime($sStartDate)); $sEndDate = gmdate("Y-m-d", strtotime($sEndDate)); // Start the variable off with the start date $aDays[] = $sStartDate; // Set a 'temp' variable, sCurrentDate, with // the start date - before beginning the loop $sCurrentDate = $sStartDate; // While the current date is less than the end date while($sCurrentDate < $sEndDate){ // Add a day to the current date $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate))); // Add this new day to the aDays array global $aDays; // deze global heb ik zelf toegevoegd en MOET hier staan, iig niet buiten deze conditie global $tot_days; $aDays[] = $sCurrentDate; } $tot_days = count($aDays); $tot_days = $tot_days +1; return $tot_days; } // function ends // // here the records are fetched from the database table while($row = $result->fetch_assoc()){ echo "<center>"; $array = implode(" / " , $row); $sStartDate = $row['created_on']; $sEndDate = date("Y-m-d"); GetDays ($sStartDate, $sEndDate); echo "</center>"; ?> <center> <form action="#" method="post" > <input type="text" name="test" value="<?php echo $array . ' / ' . $sEndDate . ' / ' . $tot_days ; ?>" size="60"/> </center> <?php } // rest of the code to complete the form --> ?>
  9. Is there a GOOGLE API for managing banners/ads ?
  10. the code is not written by me, but what do you mean with a trusted party? Is google adsense code trusted, if you have users copypaste it into your site? Mostoften they wouldnt modify it I assume. But what if .... ?
  11. The type of code I intend to store is to run banners/adverstisments.
  12. My goal is to store javascript code into a database. My first idea was to use htmlspecialchars; store it in mysql in a table column and later retrieve it with htmlspecialchars_decode. All this to prevent injection / hacking. But online I read one or two warnings that it wouldnt work, which I assume is so (I didnt test it, but it seems quite obvious afterwards) . So my question is: is it possible to have a user store javascript in a database and use it in a php script for specific purposes in a secure way?
  13. I had to change the code. I added another delimiter, namly the foreslash /. Can the [ ] be considered as a delimiter? This would mean 2 delimiters are required anyway here's my final solution: $name = preg_replace('/[^a-zA-Z]/', '', $name);
  14. I want to use preg_replace to input only a-zAZ with a limit of 30 characters I have a piece of code I used before but it works with preg_match. example function: <?php // input form to get the name // use the function here $name = valid_name($name); // function to validate name input function valid_name($data){ $data = ltrim($data); $data = rtrim($data); $data = preg_replace("/^(?:[A-Za-z][A-Za-z\-]{2,30}[A-Za-z]|[A-Za-z])$/", '' , $data); return $data; } ?> To give an impression of how I use it; a part of the confirmation form <html> <form action="mypage.php" method="post"> <p><input type="text" name="name" value="<?php echo $name; ?>" STYLE="background-color: #708DCC; color: white; height: 20px; width: 200px; border: none; font-size: 14px;" readonly ></p> <!-- rest of the form --> </html> The problem is in the preg_replace part. an input with chars like: ^&$ etc is not replaced, how to do this? if anyone can help I would be really happy.
  15. Ok thanks a lot for your help and the valuable query from davej. This really solved the problem.
  16. @davej thanks for solving, this it helped a lot. I started the script because I thought I NEEDED $sql query with placeholders, which must make it safer. like this: $sql = "SELECT id_nr, name FROM WHERE ? = id_nr;"; I use prepare statements to prevent from mysql injection, but can you tell if using palceholders is more secure then without?
  17. I have the following peace of code. I cant solve how to fetch all the records in the database. I have to use prepare but if I use place holders for $sql then it doesnt work I tried foreach and while but I dont know how to use it. Can someone please tell what code to use to fetch multiple records from a table? <?php // make connection to database $sql = "SELECT id_nr, name FROM table;"; $id_nr =1; if ($stmt = $conn->prepare($sql)) { $stmt->bind_param('i', $id_nr); $stmt->execute(); $stmt->store_result(); $num_of_rows = $stmt->num_rows; $stmt->bind_result($id_nr, $name); while ($stmt->fetch()) { echo ' id number : ' . $id_nr . '<br>'; echo ' name : ' . $name . '<br>'; } $stmt->free_result(); } $stmt->close(); mysqli_close($conn); } ?>
  18. Hi Ingolme I wondered how to use the part with ? and LIMT but couldnt find a solution. But your solutions seems to be wrong to for me too. Are there errors in it you think?
  19. How to find a record in a table? I now use : $sql = "SELECT col1 FROM table WHERE col1 = '$var' " ; if ($stmt = $conn->prepare($sql)) { $stmt->execute(); $stmt->bind_result($var); while ($stmt->fetch()) { echo $var; echo ' : this variable exists <br>'; } $stmt->close(); } to fetch $var. But this actually results in an echo of the input. The goal is not necessarily echoing it, but determining whether it exists or not and report that its not existing So is it possible with other query to determine if $var exists and then use that as TRUE ?
  20. I have a thing I dont get. I found this regex part to check for correct subdomains. function check_subdomain($data){ if (!empty($data)){ $data = preg_match("/^(?:[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9]|[A-Za-z0-9])$/", $data); return($data); } else { } } echo check_subdomain($data); but the strange thing is that echo check_subdomain($data) outputs the input and adds a 0 when wrong and a 1 when correct. why is this done and how to avoid it?
  21. @justsomeguy: You're not making it clearer to me. The function works, I drag data through it and with I can check with IF(name_of_function) { ..... if the output is ok or not.
  22. Hi thanks for reading my post. I found a solution in using only one = in if($data[0]['host']=$domain&&!empty($data[0]['target']) ) I also placed if (!empty($input)){ in the very beginning of the function. I do the programming to understand what PHP does, but some things are not easy to do just by sheer logic. I had to reduce the number of operators from 2 to 1, at php.net there is brief explanation about operators regarding equality (==) and identity (===) of the different variables, but where can you find more about using just one = . To get a better understanding of these kind of operators I would like to receive some explanation how I should interpretate it in the example I posted.
  23. My first question: I have a message like this: notice: undefined offset 0 I use an input form to check with dns_get_record and DNS_MX whether the domain of an email exists. I can check the emails if the domain does exist, but a wrong domain like: mail@verynonexistingdomain.com throws in the notice undefined offset 0 so does an empty input. <html> <center> <br> email input :<br> <form action="#" method = "post"> <input type ="text" name ="mail"> <br> <input type="submit" name = "click" value ="zend"> </form> <?php $domain =''; $data =''; var_dump($_POST['click']); var_dump($_POST['mail']); if (isset($_POST['mail'])) { echo $_POST['mail']; $input = $_POST['mail']; } // // function check_address($input){ list($user, $domain) = explode('@', $input); $data= dns_get_record($domain, DNS_MX); if($data[0]['host']==$domain&&!empty($data[0]['target'])) { return $data[0]['target']; } else { }} echo check_address($input); Echo '<br><br>'; echo $input . " : " ; if(check_address($input) ) { echo('<br>This MX records exists; I will accept this email as valid.<br> '); } else { echo('<br>No MX record exists; Invalid email.<br>'); } echo '</center>'; ?> </html> I had it working without the form part so with a constant variable, but when I added the form it threw the error. I tried several options, but cant find the logic behind executing a good input without throwing an error and a bad domain giving this notice. Anyone any idea?
  24. Hi everyone, I'm new and I like to join this forum because sometimes a perspective or hint from another programmer can be really great. My interests are PHP and MYSQL and also a bit html/css. And I do this all for fun.
×
×
  • Create New...