-
Posts
5,450 -
Joined
-
Days Won
175
Everything posted by mac_gyver
-
with the mysqli_report set, all that will do is reporting errors/throw exceptions. to display or handle the reported errors, you need to either put a try/catch block around your code and handle the thrown error yourself, or you need to have php's error_reporting set to E_ALL and display_errors set to ON so that the reported mysqli errors or the uncaught exception will be reported and displayed.
-
it's pointless to tell us that something does not work. we already know that or you wouldn't be posting on a programming help forum. we don't have the ability to run your code with your data on your server and we are not sitting right next to you to have seen what you saw when you ran your code. you have got to tell us what error or other symptom you are getting that leads you to believe that your code isn't working.
-
Submit $_POST with dynamically generated fields
mac_gyver replied to bambinou1980's topic in PHP Coding Help
this is the second time someone has questioned this. i don't think the OP has planned how his page is actually going to be used. -
Parsing and inserting multiple rows from ms sql
mac_gyver replied to rflorentino's topic in PHP Coding Help
do you really have four different data definitions stored in one table or is this an exercise to see how you would solve a made up problem? any performance problem is due to the queries and due to the number of overall queries. you would want to make sure that each query is as efficient as possible and that you run a minimum of different queries. are the queries that are using LIKE '%some_value.%' actually trying to find exact matches? if so, you should be using = comparisons. will the queries using TOP 1 only match at most one row in the table or can they match multiple rows but you only want one? do you have indexes set up in the database tables so that finding information would be as efficient as possible? so, how would i (probably) approach doing this. i would query for all your main data at once, loop over each row and split up the four different datastring formats to come up with one common set of data for each existing row, without performing any of the other select queries. insert this now, split up, common set of data into a new table. to retrieve any related data, just JOIN this new table with your TRIANGULATION, ROAD, and account tables. -
it sounds like you need to use/write a content management system, where the different content making up the pages is stored in a database table, the menu is dynamically produced based on the content that's been stored in the database table, and you have one physical page (index.php) that dynamically outputs each different logical page, when it gets requested with a page identifier as part of the url.
-
Please help me add some code for my shopping cart
mac_gyver replied to susanwiese's topic in PHP Coding Help
here's a point to ponder. if you allow multiple of any one item to be added to your cart, which you should, make sure that you provide a way of allowing the same or different engraving on each of those same items. you may want to make the entry of the engraving a separate step, where you display all the engrave-able items and allow entry of the engraving for each one. -
Please help me add some code for my shopping cart
mac_gyver replied to susanwiese's topic in PHP Coding Help
the tutorial you are following has 2-3 times too much code because the design of the cart is not efficient. it's also running a database query inside of a loop, which is killing your database server with queries. the code example that Barand posted does show you how to add the engraving input to the cart. you can modify his code to work with your cart definition or it would be better to modify your cart definition to match what he suggested. your current "add to cart" code is about 20-30 lines of code. most of that would be replaced with the few lines that he posted. you would still need to detect if $_POST['pid'] is set and cast/validate the pid value (which your current code isn't even doing) and validate the engraving value. similarity, all the other code blocks, except for the 'empty cart', can be simplified to just a few lines of code, if you simplify the definition of the cart. for the code that's retrieving and displaying the product information from the database, with the simplified cart definition, use array_keys() to get all the product id's at once, implode() that to make a comma separated list, then use that in ONE database query, using an IN() comparison in the WHERE clause to retrieve all the product information at once. you can then loop over the rows retrieved from the database and display the contents of the cart. you would use the id from the database rows to reference the quantity and engraving values stored in the cart. -
you have the wrong idea about what programming help is. we are not here to fix up the errors in your code for you. we are here to provide guidance and to point you in the right direction. if you are not at the point in your programming career to be able to take information about what is wrong and find and fix the problem yourself, you are trying to do something that is beyond your current skill set. writing (or finding and making changes to) an entire application requires that you first learn enough of the php language so that you can write error free code, then learn enough basic programming skills so that you can put the php language statements together in a meaningful way.
-
How to have a PHP script run on a server in the background
mac_gyver replied to OM2's topic in PHP Coding Help
it sounds like the browser is only used to trigger the script, not to provide any input, and that you probably run this script on a regular (scheduled) basis? if so, you would use a cron job/scheduled task to run the script. When the script finishes, it can do anything you want - send an email, update/insert data into a database table indicating when it finished and with what status, log values indicating the same type of info, ... -
that particular error means that php reached the end of your file while it was still expecting some closing php syntax. this is usually caused by a missing } but it can also be caused by a quoted string that isn't closed or even using short opening php tags when short opening php tags are not enabled.... you will need to go through your code looking for things that are not matched up. your code does have at least one serious problem. each header() redirect needs an exit; or a die; statement after the redirect to prevent the rest of the code from running while the browser is performing the redirect. the header() doesn't stop the php code. all anyone needs to do is ignore the redirect and they can stay on your page, because all that code still runs. your code also could stand to be organized better. this will reduce the total amount of code you have to write, to debug, to find missing }'s in, ... the biggest things that would help would be - 1) to put all the php code that processes form data and modifies server-side data near the start of your file. 2) put all the code that retrieves data needed to display the page next. 3) have only ONE html document, everything from the <!DOCTYPE tag through to the </html> tag, and have that last in your file. for the different content that you are repeating the html document for now, you would produce the dynamic part of that content and store it in a php variable, then echo that php variable in the ONE single html document.
-
the WEST abbreviation is correct for your time zone - date_default_timezone_set('Africa/Casablanca'); echo date_default_timezone_get() . ' => ' . date('e') . ' => ' . date('T'); results in this - this suggests that your stored data is incorrect. what's the code that's storing the data? perhaps it is altering the value before storing it?
-
Reading an MD5 hash from the database to allow users to log in
mac_gyver replied to VanityCrush's topic in PHP Coding Help
you're welcome. putting lines of code together so that they accomplish a stated goal is fundamental to this thing called programming. i can guarantee that you learned much more by actually looking at your code and fixing it yourself, than what you would have by someone telling you where to put your fingers on the keyboard and what to type. -
Reading an MD5 hash from the database to allow users to log in
mac_gyver replied to VanityCrush's topic in PHP Coding Help
if you have the hashed password stored in your database table, how can this part of your query - AND password = '$password' ever be true? -
add the new column to your table and run one UPDATE query that populates the new column from the existing column's values. if your existing data contains the commas and spaces, the format-string you use as the parameter in the STR_TO_DATE() mysql function must contain those same characters. that's why a correct design is important, so that you don't have to keep going back and fixing things.
-
one of the reasons for the yyyy-mm-dd format for a DATE data type is because that format is required in order to compare dates by magnitude. other reasons for using a DATE data type include being able to use the mysql DATE functions on the value, the most efficient data storage, and the quickest queries. you need to store your dates as a DATE data type, with that format. to insert dates that have a different format into a DATE data type, you need to reformat them. you can either do this in your php code or you can use the mysql STR_TO_DATE() function in your query.
-
Populating dropdown menu with mysql data problem
mac_gyver replied to bambinou1980's topic in Javascript Help
dynamically adding form fields is typically used when adding empty fields for data entry, such as adding a set of fields to enter the data for each additional person in a family or adding a new car to your insurance coverage... it is not used to reveal existing information, as that just adds extra clicks to the process and makes for a bad user experience on your site. if what you are wanting to do is make the select/option menu contain all the possible products (less ones that have already been picked up to that point), that's not what your code is currently doing and still makes for a 'click happy' bad user experience on your site. if what you are wanting to do is provide a way of selecting among all your products, see my reply in your other thread. -
PHP form what I can add fields with a button.
mac_gyver replied to bambinou1980's topic in PHP Coding Help
making a select/option menu that has only one choice, repeated for each product, makes no sense. what is the purpose of the select/option menu in your code? you would typically display all available products at once, ordered by category and/or name, or if you have a large number of products, provide a search box, category selection menu/links, or use pagination to limit what's being displayed at one time. the one-time non-product information for the order would normally be entered as a separate step, not as part of the product selection. you should not store the three prices in separate columns in your products table. multiple prices should be stored in another table, one row for each price, tied back to the products table using the product id. you can then store any number of prices for any product. when you JOIN the two tables to display the information, you will only get a row in the result set where there is a price. your code to produce the (three) radio buttons would simply loop over however many rows the JOINed query returns and produce that number or radio buttons. any time you find yourself repeating code, that only differs in the value it uses (such as your 3 radio button logic), it's a sign that you should be using a loop of some kind rather than writing out the code n number of times for each possible input value. before you move up to using jquery/ajax, you need to be able to produce the client side and server side code that accomplishes your task, since you will still need all of that client and server code when using jquery/ajax. jquery/ajax isn't required to make certain types of pages work. they only allow you to dynamically do things in the client without refreshing the page. without using them only means that when you perform an action in the client, the resulting output completely comes from the server. -
the following is equivalent, without the extra $myarray that's causing all the data to be stacked together, to what you are showing - while ($row = $result->fetch_assoc()) { file_put_contents("message/{$row['id']}.json", json_encode($row)); }
-
yes, your errors are due to the debugger. php code debuggers generally work by adding a layer of output buffing to send the debugging information to their client module. you should generally only use a debugger to debug why your code isn't doing what you expect (that's why they are called debuggers), not as the primary method of running your code.
-
php include/require statements, unless you specify the syntax for a url (protocol, domain, path, file - i.e. http://your_domain/your_path/your_file.php), operate on files through the file system, where url get parameters - ?some_name=some_value have no meaning. however, you would in general not want to use a url, because it is slow (your web server makes a http request back to your own web server), you only get the OUTPUT from your file, not the actual php code, and the two php settings that are required for a url to work are turned off by default due to the security problem of this allowing unvalidated external values being used to specify included files to allow a hacker to include their remote code onto your server and run it on your server. if your goal is to retrieve some information based on an id and make that information available to the 'calling' code, you would write a function that accepts the id as a parameter and returns the data to the calling program.
-
because you haven't posted any actual output from your code, there's a chance that the error is in your interpretation of the result. also, unix timestamps are not reliable, since an actual conversion between different number systems is required to use them. the following is from mysql's own documentation for it's unix timestamp based functions - why not just store a mysql DATETIME value?
-
to fix your debatable design, you don't need to run the query that doesn't work/times out. you need to select all the rows from the badly designed table, that has the multiple string values stored in the single column, split up those values, get or assign an auto-increment integer id key for each unique string value, then insert row(s), one for each existing row and each original split values, into a new table, with the id corresponding to the string and the id of whatever you are relating that information too. the resulting table should only have integer id values in it. for example, if you have a row in your existing table with only one of these string values stored in the offending column, you would end up inserting one row in the new table. if you have a row in your existing table with three of these string values stored in the single column, you would end up inserting three rows in the new table. once you get finished with this deconstruction/reconstruction process, you would use this new table in JOINed queries to relate, using exact value, integer, matches, the source data in the parent tables. then, by using correct indexes on the tables, you can easily and quickly query tables that contain several millions of rows.
-
Help with designing my mysql tables before coding in PHP
mac_gyver replied to bambinou1980's topic in MySQL Help
in real life applications, data is not actually deleted. if you insert a row into a table and assign it an id (identifier), that row is never deleted, so that any data that uses that id will always be valid. if you want to make a piece of data unavailable at some point, you would have a status column that controls if it can be chosen. yes, you would have a price table that contains the product_id, customer type, and since the price for anything can change over time, a start date and end data that the stored price is applicable. -
the following is your code, rearranged as suggested - <?php // initialization $conn = mysqli_connect("localhost","root","","hsa_project_hub"); session_start(); // user state/permission check if(!$_SESSION['user_loginName']){ header("location: index.php"); exit; // in one your other threads, it was suggested that you add this here. this elimiates the need for the else {} statement - } // else { // not needed if you exit after the header() redirect. you also don't have the closing } in your code in the correct place, so it's best to simply elimiate unnecessary code. i have removed the closing } from this example code. // post method form processing if($_SERVER['REQUEST_METHOD'] == 'POST') // an overall check if a post method form was submitted { //Enter info in database if(isset($_POST['submit_data'])){ //Getting the form information and saving in local variables // $name = mysqli_real_escape_string($conn,$_POST['name']); // this is the RFI number and should be produced by the database $subject = mysqli_real_escape_string($conn,$_POST['subject']); $date_submit = mysqli_real_escape_string($conn,$_POST['date_submit']); $needed_by = mysqli_real_escape_string($conn,$_POST['needed_by']); $question = mysqli_real_escape_string($conn,$_POST['question']); $gc_rsp = mysqli_real_escape_string($conn,$_POST['gc_rsp']); $owner_rsp = mysqli_real_escape_string($conn,$_POST['owner_rsp']); $engr_rsp = mysqli_real_escape_string($conn,$_POST['engr_rsp']); $arch_rsp = mysqli_real_escape_string($conn,$_POST['arch_rsp']); $final_by = mysqli_real_escape_string($conn,$_POST['final_by']); $date_returned = mysqli_real_escape_string($conn,$_POST['date_returned']); $status = mysqli_real_escape_string($conn,$_POST['status']); // Creating local variable for query from Session variable $first_name = $_SESSION['firstName']; $project_id = $_SESSION['project_id']; $sql = " INSERT INTO rfis ( id, name, subject, issued_by, date_submit, needed_by, question, gc_rsp, owner_rsp, engr_rsp, arch_rsp, final_by, date_returned, status ) VALUES ( '$project_id', '$name', '$subject', '$first_name', '$date_submit', '$needed_by', '$question', '$gc_rsp', '$owner_rsp', '$engr_rsp', '$arch_rsp', '$final_by', '$date_returned', '$status' ) "; if (mysqli_query($conn, $sql)) { echo "Success with first query"; // at this point you can get the auto-increment id that was assigned to the inserted row // all the code that's dependetn on the INSERT query working would need to be here or would need to check for errors from the above code (you would have to actually set elements in the $errors array to do this.) } else { // you ALWAYS need to handle errors. if you use trigger_error() you can display them when devloping/debugging and log them on a live server since trigger_error() uses php's error_reporting/display_errors/log_errors settings //echo "Error: " . $sql . "<br>" . mysqli_error($conn); } } // the following section of code goes-a-way once you use the auto-increment value from the INSERT query //Fetch new RFI No. from database /* $s = "SELECT rfis.id,rfis.name\n" . "FROM rfis\n" . "where rfis.id=$project_id\n" . "and rfis.name=$name"; $result=mysqli_query($conn,$s); $row=mysqli_fetch_assoc($result); $i=0; $run = mysqli_query($conn, $s); //assign values to local variables while($row=mysqli_fetch_array($run)){ $rfiNo =$row["name"]; $i++; } */ // the following code is part of the form processing. you should validate the uploaded file infomration when you validate the other form data. // at this point, you will have the auto-increment id value from the INSERT query above. // process uploaded files if(isset($_FILES['files'])){ // unless you exceed the post max size setting, this will be set if the form was submitted. $errors= array(); foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; if($file_size > 2097152){ $errors[]='File size must be less than 2 MB'; } $query="INSERT into rfi_files(`rfi_id`,`rfi_project_id`,`rfi_fileName`,`rfi_fileSize`,`rfi_fileType`) VALUES('$rfiNo','$project_id','$file_name','$file_size','$file_type'); "; $desired_dir="rfi_files"; if(empty($errors)==true){ if(is_dir($desired_dir)==false){ mkdir("$desired_dir", 0700); // Create directory if it does not exist } if(is_dir("$desired_dir/".$file_name)==false){ move_uploaded_file($file_tmp,"$desired_dir/".$file_name); }else{ // rename the file if another one exist $new_dir="$desired_dir/".$file_name.time(); rename($file_tmp,$new_dir) ; } mysqli_query($conn,$query); }else{ print_r($errors); } } if(empty($error)){ // you have a typo in this line, it should be $errors echo "<script>alert('RFI $rfiNo added successfully')</script>"; echo "<script>window.open('rfi_list.php','_self')</script>"; } } } // get method business logic (produces/retrieves data needed to display the page) // in this case, you are not producing/retrieving any data (if you were editing an existing record, you would retreive that data here...) // the dividing line between the business logic and the presentation logic. you can close any database connection since you are done retreiving any database data at this point. // get method presentation logic (uses the data from the business logic and produces the dynamic output for the page) // if the output doesn't require any heavy processing/formatting, just use the data directly in the html page/template code. // again, not used in this case // html page/template (the actual html document that the dynamic output is put into to make the complete page.) // only simple php conditional logic/loops and echo statements should be present beyond this point. ?> <!DOCTYPE HTML> <html> <head> <title>New RFI</title> <link href="hsastyle.css" rel="stylesheet"> <link href='http://fonts.googleapis.com/css?family=Josefin+Sans' rel='stylesheet' type='text/css'> <style> form label { float: left; width: 150px; margin-bottom: 5px; margin-top: 5px; padding: 5px; } .clear { display: block; clear: both; width: 100%; } #rfi_container { background: #F0F0F0; font-family: 'Josefin Sans', sans-serif; font-weight: bold; color: #8F0000; } #files { background: yellow; display:block; } </style> </head> <body> <div id="main_container"> <p><em>version 1.0 beta</em></p> <div id="banner"> <div id="logo"> <img src="images/hsa-logo.jpg" alt=HSA logo> </div> <div id="logout"><H5><a href="logout.php">Log Out</a></H5></div> <div id="welcome"><h6>Welcome <?php echo $_SESSION['firstName'];?></h6></div> <div id="project_name"> <strong><em><?php echo $_SESSION['projName']?></em></strong> </div> </div> <!--End Banner--> <div id="user_list"> <FORM> <INPUT Type="BUTTON" Value="Back to RFI List" Onclick="window.location.href='rfi_list.php'"> </FORM> </div> <div id="rfi_container"> <!--Create RFI for user input--> <form id="form1" name="form1" method="post" action="new_rfi.php" enctype="multipart/form-data"><br> <br class="clear" /> <label for="name">RFI NO.</label><input type="text" name="name" id="name" placeholder="Ex: 003" required="required" /> <br class="clear" /> <label for="subject">Subject</label><input type="text" name="subject" id="subject" placeholder="Ex: Discontinued floor finish" /> <br class="clear" /> <label for="date_submit">Date submitted:</label><input type="date" name="date_submit" id="date_submit" /> <br class="clear" /> <label for="needed_by">Date Needed by:</label><input type="date" name="needed_by" id="needed_by" /> <br class="clear" /> <label for="question">Question:</label><textarea name="question" id="question" required="required" cols="100" rows="5"></textarea> <br class="clear" /> <label for="gc_rsp">Contractor Suggestion:</label><textarea name="gc_rsp" id="gc_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="owner_rsp">Owner Response:</label><textarea name="owner_rsp" id="owner_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="engr_rsp">Engineer Response:</label><textarea name="engr_rsp" id="engr_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="arch_rsp">Architect Response:</label><textarea name="arch_rsp" id="arch_rsp" cols="100" rows="5"></textarea><br> <br class="clear" /> <label for="final_by">Final Response by:</label><select name="final_by" id="final_by"> <option></option> <option value="Architect">Architect</option> <option value="Owner">Owner</option> <option value="Structural Engineer">Structural Engineer</option> <option value="MEP Engineer">MEP Engineer</option> <option value="Civil Engineer">Civil Engineer</option> <option value="Landscape Architect">Landscape Architect</option> </select> <br class="clear" /> <label for="date_returned">Date Returned:</label><input type="date" name="date_returned" id="date_returned" /><br> <br class="clear" /> <label for="status">Status:</label><select name="status" id="status"> <option></option> <option value="CLOSED">CLOSED</option> <option value="at Architect">at Architect</option> <option value="at MEP Engineer">at MEP Engineer</option> <option value="at Structural Engineer">at Structural Engineer</option> <option value="at Civil Engineer">at Civil Engineer</option> <option value="at Landscape Architect">at Landscape Architect</option> <option value="at Owner">at Owner</option> <option value="at General Contractor">at General Contractor</option> </select> <br class="clear" /><br> <input type="file" name="files[]" multiple="" /> <br class="clear" /><br> <center><input type="submit" name="submit_data" id="submit_data" value="Submit RFI" /></center> </form> <br> <!--<div id="files"><a href="rfi_files<?php// echo $rfiFilename;?>"><?php //echo $rfiFilename;?></a></div> USE LATER TO SHOW LINK FOR DOWNLOAD--> <div><!--end of container--> </body> </html> note: i didn't actually FIX anything in this code, it may in fact have php syntax errors. other than a few minor changes, which are commented, there are only comments added to this code or commented out code.
-
some things to consider - 1) this form/form processing is for creating a RFI. it shouldn't be up to the user to enter/manage the RFI number (and if he was, the form field name, php variables, and database column name should all match and have a name the reflects the purpose of the value.) you should use an auto-increment column in your database table to manage the RFI number. 2) ALL the form processing code that belongs to one form should be together, inside one logic test. your code around line 200 isn't even inside of any form processing logic and will be executed each time the page gets requested. 3) you need to arrange your code better. any post method form processing code should come near the start of your file. your code should be laid out in this general order - initialization, user state/permission check, post method form processing, get method business logic (produces/retrieves data needed to display the page), get method presentation logic (uses the data from the business logic and produces the dynamic output for the page), html page/template (the actual html document that the dynamic output is put into to make the complete page.) doing this will reduce the amount of code, because it will consolidate the different sections that you now have scattered throughout your file. it will also make it easier to test each section and then to only post the relevant section if you need help. so, if the database is creating/managing the RFI number, after you successful INSERT the new row, you can retrieve the just assigned auto-increment id by calling mysqli_insert_id($conn)